home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgambit-20-compiler-src-p1 / Online Help < prev    next >
Encoding:
Text File  |  1994-07-26  |  172.8 KB  |  4,184 lines  |  [TEXT/????]

  1. MacGambit
  2. A. Introduction    0
  3. -    0
  4. B. Using the online help    1247
  5. -    0
  6. C. User interface    2595
  7. C.1. Interacting with the interpreter    2617
  8. C.2. Basic editing    7797
  9. C.3. Special keys    8688
  10. C.4. Arrow keys    9194
  11. C.5. Emacs commands    10642
  12. C.6. Small keyboard emacs mode    11774
  13. C.7. Mouse    12888
  14. C.8. Scrolling    13505
  15. C.9. Editor limitations    13720
  16. -    0
  17. D. Language extensions    13924
  18. D.1. Concurrent evaluation    14059
  19. D.2. Semaphores    15302
  20. D.3. Queues    15838
  21. D.4. Weak Pairs    16665
  22. D.5. Ports    17502
  23. D.6. Symbols    20082
  24. D.7. Evaluation    20862
  25. D.8. Tracing    22400
  26. D.9. Miscellaneous    22798
  27. D.10. Graphics    23861
  28. D.11. Text windows    24963
  29. D.12. Toolbox interface    25444
  30.  
  31. R4RS
  32. 1. Overview of Scheme    29391
  33. 1.1. Semantics    29417
  34. 1.2. Syntax    32799
  35. 1.3. Notation and terminology    33400
  36. 1.3.1. Essential and non-essential features    33434
  37. 1.3.2. Error situations and unspecified behavior    34047
  38. 1.3.3. Entry format    35651
  39. 1.3.4. Evaluation examples    38602
  40. 1.3.5. Naming conventions    39121
  41. -    0
  42. 2. Lexical conventions    39803
  43. 2.1. Identifiers    40191
  44. 2.2. Whitespace and comments    42089
  45. 2.3. Other notations    43131
  46. -    0
  47. 3. Basic concepts    44760
  48. 3.1. Variables and regions    44782
  49. 3.2. True and false    46700
  50. 3.3. External representations    47110
  51. 3.4. Disjointness of types    49267
  52. 3.5. Storage model    49625
  53. -    0
  54. 4. Expressions    51311
  55. 4.1. Primitive expression types    51881
  56. 4.1.1. Variable references    51917
  57. 4.1.2. Literal expressions    52322
  58. 4.1.3. Procedure calls    53856
  59. 4.1.4. Lambda expressions    55381
  60. 4.1.5. Conditionals    57790
  61. 4.1.6. Assignments    58593
  62. 4.2. Derived expression types    59112
  63. 4.2.1. Conditionals    59323
  64. 4.2.2. Binding constructs    63696
  65. 4.2.3. Sequencing    67469
  66. 4.2.4. Iteration    68084
  67. 4.2.5. Delayed evaluation    71120
  68. 4.2.6. Quasiquotation    71633
  69. -    0
  70. 5. Program structure    74339
  71. 5.1. Programs    74364
  72. 5.2. Definitions    75249
  73. 5.2.1. Top level definitions    76318
  74. 5.2.2. Internal definitions    77229
  75. -    0
  76. 6. Standard procedures    78431
  77. 6.1. Booleans    78923
  78. 6.2. Equivalence predicates    80929
  79. 6.3. Pairs and lists    89220
  80. 6.4. Symbols    100276
  81. 6.5. Numbers    104239
  82. 6.5.1. Numerical types    105260
  83. 6.5.2. Exactness    106926
  84. 6.5.3. Implementation restrictions    108495
  85. 6.5.4. Syntax of numerical constants    112972
  86. 6.5.5. Numerical operations    114811
  87. 6.5.6. Numerical input and output    130979
  88. 6.6. Characters    134169
  89. 6.7. Strings    139477
  90. 6.8. Vectors    145492
  91. 6.9. Control features    149072
  92. 6.10. Input and output    159159
  93. 6.10.1. Ports    159186
  94. 6.10.2. Input    162997
  95. 6.10.3. Output    166744
  96. 6.10.4. System interface    168994
  97. -    0
  98. 7. Formal syntax and semantics    170485
  99.  
  100. A-B
  101. abs    121317
  102. acos    126325
  103. and    b62459
  104. angle    128861
  105. append    96331
  106. apply    149743
  107. asin    126325
  108. assoc    98905
  109. assq    98905
  110. assv    98905
  111. atan    126325
  112. -    0
  113. begin    b67469
  114. boolean?    80637
  115.  
  116. C
  117. c...r    94681
  118. call-with-current-continuation    155293
  119. call-with-input-file    159401
  120. call-with-output-file    159401
  121. car    93467
  122. case    b61015
  123. cdr    93828
  124. ceiling    124061
  125. char->integer    138260
  126. char-alphabetic?    137387
  127. char-ci<=?    136667
  128. char-ci<?    136667
  129. char-ci=?    136667
  130. char-ci>=?    136667
  131. char-ci>?    136667
  132. char-downcase    139094
  133. char-lower-case?    137387
  134. char-numeric?    137387
  135. char-ready?    165775
  136. char-upcase    139094
  137. char-upper-case?    137387
  138. char-whitespace?    137387
  139. char<=?    135595
  140. char<?    135595
  141. char=?    135595
  142. char>=?    135595
  143. char>?    135595
  144. char?    135450
  145. clear-graphics    i23880
  146. clear-point    i23880
  147. close-input-port    162622
  148. close-output-port    162622
  149. compile-file    i21236
  150. complex?    115417
  151. cond    b59323
  152. cons    92972
  153. cos    126325
  154. current-input-port    160923
  155. current-output-port    160923
  156.  
  157. D-F
  158. define    b75249
  159. delay    b71120
  160. denominator    123525
  161. display    167307
  162. do    b68084
  163. draw-line-to    i23880
  164. draw-point    i23880
  165. -    0
  166. else    b59323
  167. eof-object?    165453
  168. eq?    86628
  169. equal?    88456
  170. eqv?    81368
  171. error    i22821
  172. eval    i20882
  173. even?    118636
  174. exact->inexact    130105
  175. exact?    117297
  176. exit    i23342
  177. exp    126325
  178. expt    128675
  179. -    0
  180. floor    124061
  181. for-each    151309
  182. force    151915
  183. future    ib14059
  184.  
  185. G-L
  186. gcd    123005
  187. gensym    i20099
  188. get    i20412
  189. get-output-string    i17517
  190. graphics-text    i23880
  191. -    0
  192. if    b57790
  193. imag-part    128861
  194. inexact->exact    130105
  195. inexact?    117297
  196. input-port?    160668
  197. integer->char    138260
  198. integer?    115417
  199. -    0
  200. lambda    b55381
  201. lcm    123005
  202. length    96095
  203. let    b63696
  204. let*    b65198
  205. letrec    b65878
  206. list    95872
  207. list->string    144753
  208. list->vector    148383
  209. list-ref    97599
  210. list-tail    97282
  211. list?    95436
  212. load    169191
  213. log    126325
  214.  
  215. M-N
  216. magnitude    128861
  217. make-polar    128861
  218. make-queue    i15854
  219. make-rectangular    128861
  220. make-semaphore    i15322
  221. make-string    141132
  222. make-vector    146787
  223. map    150408
  224. max    119142
  225. member    97915
  226. memq    97915
  227. memv    97915
  228. min    119142
  229. modulo    121486
  230. -    0
  231. negative?    118636
  232. newline    168184
  233. not    80199
  234. null?    95288
  235. number->string    131017
  236. number?    115417
  237. numerator    123525
  238.  
  239. O-R
  240. odd?    118636
  241. open-input-file    162026
  242. open-input-string    i17517
  243. open-output-file    162276
  244. open-output-string    i17517
  245. open-text-window    i24986
  246. or    b63065
  247. output-port?    160668
  248. -    0
  249. pair?    92654
  250. peek-char    164546
  251. position-pen    i23880
  252. positive?    118636
  253. pp    i19384
  254. pretty-print    i19384
  255. procedure?    149263
  256. put    i20412
  257. -    0
  258. quasiquote    b71633
  259. queue-get!    i15854
  260. queue-put!    i15854
  261. quote    b52322
  262. quotient    121486
  263. -    0
  264. rational?    115417
  265. rationalize    125529
  266. read    163015
  267. read-char    164101
  268. real-part    128861
  269. real?    115417
  270. remainder    121486
  271. reverse    96986
  272. round    124061
  273. runtime    i23459
  274.  
  275. S
  276. semaphore-signal    i15322
  277. semaphore-wait    i15322
  278. set!    b58593
  279. set-car!    94153
  280. set-cdr!    94507
  281. set-gc-report    i23607
  282. sin    126325
  283. sqrt    128454
  284. string    141492
  285. string->list    144753
  286. string->number    132602
  287. string->symbol    103276
  288. string-append    144572
  289. string-ci<=?    142925
  290. string-ci<?    142925
  291. string-ci=?    142462
  292. string-ci>=?    142925
  293. string-ci>?    142925
  294. string-copy    145188
  295. string-fill!    145323
  296. string-length    141637
  297. string-ref    141777
  298. string-set!    141975
  299. string<=?    142925
  300. string<?    142925
  301. string=?    142462
  302. string>=?    142925
  303. string>?    142925
  304. string?    140990
  305. substring    144178
  306. symbol->string    102205
  307. symbol?    101815
  308.  
  309. T-Z
  310. tan    126325
  311. touch    i15083
  312. trace    ib22400
  313. transcript-off    169825
  314. transcript-on    169825
  315. truncate    124061
  316. -    0
  317. untrace    ib22400
  318. -    0
  319. vector    147136
  320. vector->list    148383
  321. vector-fill!    148892
  322. vector-length    147368
  323. vector-ref    147495
  324. vector-set!    147905
  325. vector?    146645
  326. -    0
  327. weak-car    i16685
  328. weak-cdr    i16685
  329. weak-cons    i16685
  330. weak-pair?    i16685
  331. weak-set-car!    i16685
  332. weak-set-cdr!    i16685
  333. with-input-from-file    161130
  334. with-input-from-port    i19052
  335. with-input-from-string    i18419
  336. with-output-to-file    161130
  337. with-output-to-port    i19052
  338. with-output-to-string    i18419
  339. write    166763
  340. write-char    168588
  341. -    0
  342. zero?    118636
  343.  
  344. Others
  345. '    b52322
  346. *    119958
  347. +    119958
  348. ,    b71633
  349. –    120392
  350. /    120392
  351. ;    b42089
  352. <    117598
  353. <=    117598
  354. =    117598
  355. =>    b59323
  356. >    117598
  357. >=    117598
  358. `    b71633
  359.  
  360.  
  361. ÛA. Introductionˇ
  362.  
  363. MacGambit is a full implementation of Scheme that conforms to the IEEE-Scheme standard
  364. (IEEE P1178) and to the Revised˝4ˇ Report on Scheme (R4RS). The system supports the whole
  365. numeric tower (i.e. integer, rational, real and complex numbers). It also has several
  366. extensions to the standards including: weak pairs, string ports, property lists, pretty
  367. printer, debugger, compiler and multitasking. Macintosh specific features include: a Scheme
  368. interface to several Toolbox routines, a drawing window for simple graphics, an online help
  369. system and a Scheme oriented editor with an emacs compatibility mode.
  370.  
  371. MacGambit runs on all Macintosh computers but requires at least 2 MB of RAM and System 6
  372. or later. When the program is launched, it grabs most of the memory for its heap and leaves
  373. the rest for the Mac's own needs. When running Multifinder or System 7, it is possible to
  374. limit the amount of memory allocated by setting the "application memory size" (with the
  375. finder's Ícmd-Iˇ command).
  376.  
  377. The latest version of MacGambit can be obtained through the INTERNET from the following
  378. anonymous FTP site:
  379.  
  380. Í     trex.iro.umontreal.ca:pub/gambitˇ
  381.  
  382. Email concerning MacGambit should be sent to:
  383.  
  384. Í     gambit@trex.iro.umontreal.caˇ
  385.  
  386.  
  387. ÛB. Using the online helpˇ
  388.  
  389. The online help contains two documents. The first document (MacGambit) contains
  390. information that is specific to MacGambit. The second document (R4RS) is a definition of the
  391. Scheme programming language. The first two menus of the online help window are the
  392. respective table of contents of these documents. The remaining menus are an alphabetic
  393. index of the procedures and special forms available. Procedures are in plain face, special
  394. forms are in bold face and MacGambit specific extensions are in italic.
  395.  
  396. Navigating through the documentation is done either by selecting a topic from one of the help
  397. window's menus or by calling the procedure Íhelpˇ. When given the name of a procedure or
  398. special form (as a string or symbol), Íhelpˇ will move the help window to the appropriate
  399. part of the documentation.
  400.  
  401. The main chapters of R4RS have been reproduced as faithfully as possible including the
  402. formatting conventions. However a few sections were omitted for space reasons: the
  403. summary, introduction, chapter 7 and appendices. We thank the editors of R4RS, William
  404. Clinger and Jonathan Rees, for making the report publicly available. The complete R4RS
  405. document can be obtained from the following anonymous FTP sites:
  406.  
  407. Í     nexus.yorku.ca:pub/scheme/doc/r4rs.ps.Zˇ
  408. Í     altdorf.ai.mit.edu:pub/scheme-reports/r4rs.psˇ
  409.  
  410.  
  411. ÛC. User interfaceˇ
  412.  
  413.  
  414. ÛC.1. Interacting with the interpreterˇ
  415.  
  416. Interaction with the interpreter's read-eval-print (REP) loop is done inside the "interaction"
  417. window. The content of the interaction window can be edited at will by the user. This window
  418. also acts as the default output port (and input port). Consequently, the interpreter sends its
  419. output to the interaction window. User input and system output are displayed with different
  420. fonts. By default, user input is in bold face (but this and other attributes of the window can
  421. be changed with the "Styles..." item in the Edit menu).
  422.  
  423. Normally, the user types an expression and then "sends" it to the interpreter to be
  424. evaluated. The result of the evaluation is then output to the interaction window. The
  425. preferred way of evaluating an expression is to place the insertion point (caret) immediately
  426. after the expression to evaluate and then typing Ícmd-returnˇ (i.e. press the Íreturnˇ key
  427. while pressing the Ícommandˇ key). An alternative method is to use the Íenterˇ key. When
  428. Íenterˇ is pressed, the sequence of "user input" characters immediately preceding the
  429. insertion point are sent to the interpreter. Because it doesn't check for a properly formed
  430. expression, the Íenterˇ key is useful for programs doing text oriented input. Arbitrary text
  431. can also be sent to the interpreter by selecting the text before typing Ícmd-returnˇ or
  432. Íenterˇ.
  433.  
  434. These interaction mechanisms also work from windows associated to files. However, the
  435. result of evaluation is always displayed in the interaction window.
  436.  
  437. As opposed to most other Lisp systems, MacGambit combines the standard REP loop functions
  438. with those of the debugger. In a sense, the user is always interacting with the debugger.
  439. When an evaluation error is detected by the interpreter, a message describing the error is
  440. displayed and a REP loop of a higher level is started (the topmost REP loop is numbered 0).
  441. This REP loop is positionned in the context of the error so that the user can determine the
  442. cause of the error and possibly repair the problem and resume the evaluation as though no
  443. error had occured.
  444.  
  445. At all times it is possible to examine the frames in the REP loop's continuation (i.e. the call
  446. stack). This is useful to locate the source of an error. Expressions typed in to the REP loop
  447. are evaluated in the context of the frame currently being examined. This is useful to check
  448. the bindings of variables and mutate them (check the examples for details). Special
  449. commands accepted by a REP loop are:
  450.  
  451. Ícmd-Rˇ -- Returns from an error or interrupt (the user is prompted for the return value).
  452. This value is ignored when returning from an interrupt.
  453.  
  454. Ícmd-Dˇ -- Returns to the previous REP loop (also acts as end-of-file).
  455.  
  456. Ícmd-Tˇ -- Returns to the top-most REP loop.
  457.  
  458. Ícmd-?ˇ -- Gives summary of commands.
  459.  
  460. Ícmd-+ˇ -- Moves to next frame of the continuation and displays it in the same format as
  461. Ícmd-Bˇ.
  462.  
  463. Ícmd--ˇ -- Moves to previous frame of the continuation and displays it in the same format as
  464. Ícmd-Bˇ.
  465.  
  466. Ícmd-Bˇ -- Displays a summary of each frame in the continuation starting with the current
  467. frame. There are 3 columns. The first is the frame number. The second is the procedure that
  468. created the frame (and where control will return when this frame is resumed). The last
  469. column, if it is present, is the expression whose value is being computed. A frame created
  470. by a compiled procedure has a third column only if the procedure was compiled with the
  471. DEBUG option.
  472.  
  473. Ícmd-Lˇ -- Lists all non-global variables accessible from the current frame.
  474.  
  475. Ícmd-Iˇ -- Pretty prints the procedure that created the current frame.
  476.  
  477. Ícmd-Yˇ -- Displays subproblem expression for the current frame.
  478.  
  479. Ícmd-.ˇ -- This can be typed at any moment to interrupt the current computation. A new REP
  480. loop is started.
  481.  
  482. Ícmd-Jˇ -- Loads a file.
  483.  
  484. Ícmd-Kˇ -- Compiles a file.
  485.  
  486. Here is a transcript of a typical interaction with MacGambit (comments are marked with
  487. ";;;" and user input is in bold):
  488.  
  489. ÌMacGambit (v2.0)  
  490. Ì
  491. Ì: Í(define (f x y) (/ (+ x y) 2))Ì
  492. Ìf
  493. Ì
  494. Ì: Í(let ((a 10) (b 20))
  495. Í    (* (f a 'b) (f a 100)))Ì ;;; we meant b not 'b
  496. Ì*** ERROR -- NUMBER expected
  497. Ì(+ 10 'b)
  498. Ì
  499. Ì1: ;;; typed Ícmd-BÌ to get backtrace
  500. Ì0    f                     (+ x y)
  501. Ì-1   (top level)           (f a 'b)
  502. Ì-2   (top level)           (let ((a 10) (b 20)) (* (f a 'b)...
  503. Ì-3   ##dynamic-env-bind
  504. Ì-4   ##read-eval-print
  505. Ì-5   ##dynamic-env-bind
  506. Ì-6   ###_kernel.startup
  507. Ì
  508. Ì1: ;;; typed Ícmd-IÌ to get the procedure that caused the error
  509. Ì#[procedure f] =
  510. Ì(lambda (x y) (/ (+ x y) 2))
  511. Ì
  512. Ì1: ;;; typed Ícmd-LÌ to get the variables accessible here
  513. Ìx = 10
  514. Ìy = b
  515. Ì
  516. Ì1: ;;; typed Ícmd--Ì to move to the previous subproblem
  517. Ì-1   (top level)           (f a 'b)
  518. Ì
  519. Ì1-1: ;;; typed Ícmd-LÌ
  520. Ìa = 10
  521. Ìb = 20
  522. Ì
  523. Ì1-1: ;;; typed Ícmd-+Ì
  524. Ì0    f                     (+ x y)
  525. Ì
  526. Ì1: ;;; typed Ícmd-RÌ to return from error
  527. ÌReturn value: Í(+ x 20)Ì
  528. Ì825
  529. Ì
  530. Ì: ;;; typed Ícmd-QÌ
  531. Ìˇ
  532.  
  533. The file "init.scm" can be used to customize the interpreter. It is loaded just before the
  534. initial REP loop is entered. The file is first searched in the folder containing the document
  535. being opened and then in the folder containing the interpreter.
  536.  
  537.  
  538. ÛC.2. Basic editingˇ
  539.  
  540. It is assumed that you are familiar with standard Macintosh editing techniques. If not, you
  541. should consult your Getting Started disks and Macintosh users' manuals.
  542.  
  543. On all keyboards the following keys are defined:
  544.  
  545. Í    delete -ˇ erase character before cursor position
  546. Í       tab -ˇ indent current line
  547. Í    return -ˇ insert a new line and move to the left margin (and then
  548. Í            ˇ do a Ítabˇ if autoindent is selected for the window)
  549. Ícmd-return -ˇ send the expression preceding the insertion point
  550. Í     enter -ˇ send the user input preceding the insertion point
  551.  
  552. Printable characters which are legal in Scheme programs are simply inserted into the buffer
  553. (these are characters in the range of 32 through 126 in the ASCII table). Other characters
  554. are simply discarded. However, you can paste arbitrary characters from another application
  555. or desk accessory.
  556.  
  557.  
  558. ÛC.3. Special keysˇ
  559.  
  560. On extended keyboards the following special keys are supported:
  561.  
  562. Í   F1 - F4 -ˇ equivalent to Undo, Cut, Copy and Paste
  563. Í  F5 - F15 -ˇ function keys (not used)
  564. Í      del> -ˇ erase character after the insertion point
  565. Í      home -ˇ scroll text to the top of the edit buffer
  566. Í       end -ˇ scroll text to the bottom of the edit buffer
  567. Í   page up -ˇ scroll text up a page
  568. Í page down -ˇ scroll text down a page
  569.  
  570. Note that these last four do not move the insertion point, just scroll.
  571.  
  572.  
  573. ÛC.4. Arrow keysˇ
  574.  
  575. Arrow keys (when available) move the insertion point in the corresponding direction:
  576.  
  577. Í      left -ˇ move cursor to the left one character
  578. Í     right -ˇ move cursor to the right one character
  579. Í        up -ˇ move cursor up one line
  580. Í      down -ˇ move cursor down one line
  581.  
  582. Arrow keys can be modified with the Íshiftˇ, Íoptionˇ, and Ícommandˇ keys.
  583.  
  584. The Íshiftˇ key may be used to extend the text selection per Apple standards. Each selection
  585. has an anchor end and an active end. Generally, the active end is the last end you moved, the
  586. anchor end doesn't move. When the Íshiftˇ key is held down, the selection's active end is
  587. moved by the arrow keys.
  588.  
  589. The Íoptionˇ key may be used with the arrow keys to move the insertion point left or right
  590. by word, or up or down by page. ÍShiftˇ and Íoptionˇ together may be used to move the
  591. active end of the selection left or right by words, or up or down by pages.
  592.  
  593. The Ícommandˇ key may be used with the arrow keys to move the insertion point left or right
  594. to the beginning or end of the line, or up or down to the beginning or end of the text. ÍShiftˇ
  595. and Ícommandˇ together may be used to move the active end of the selection to start or end of
  596. the line or text.
  597.  
  598. When Íshiftˇ, Íoptionˇ, and Ícommandˇ are held down together, the up or left arrow will make
  599. the beginning of the selection the active end, and the down or right arrow will make the end
  600. of the selection the active end.
  601.  
  602.  
  603. ÛC.5. Emacs commandsˇ
  604.  
  605. A small subset of Emacs compatible command keys are supported by the editor. Some of
  606. these commands make use of the Ícontrolˇ key. The Ícontrolˇ key does not exist on small
  607. keyboards but it can be simulated by using the Small Keyboard mode (see below).
  608.  
  609. The following table lists the Emacs compatible command keys using the following
  610. abbreviations: ÍCˇ = Ícontrolˇ key, ÍMˇ = Íoptionˇ key (Emacs' "meta" key).
  611.  
  612. Commands which move the insertion point:
  613.  
  614. Í  C-f      -ˇ forward character
  615. Í  C-M-f    -ˇ forward S-expression
  616. Í  C-b      -ˇ backward character
  617. Í  C-M-b    -ˇ backward S-expression
  618. Í  C-a      -ˇ beginning of line
  619. Í  C-M-a    -ˇ beginning of S-expression
  620. Í  C-m      -ˇ select enclosing S-expression
  621. Í  C-M-m    -ˇ select enclosing S-expression
  622. Í  C-e      -ˇ end of line
  623. Í  C-M-e    -ˇ end of S-expression
  624. Í  C-p      -ˇ previous line
  625. Í  C-n      -ˇ next line
  626.  
  627. Commands which modify the buffer:
  628.  
  629. Í  C-d      -ˇ delete forward (same as Ídel>ˇ key)
  630. Í  C-del>   -ˇ delete to end of line
  631. Í  C-k      -ˇ same as ÍC-del>ˇ
  632. Í  C-o      -ˇ open line
  633.  
  634. Other commands: Í  M-return -ˇ same as Ícmd-returnˇ
  635.  
  636.  
  637. ÛC.6. Small keyboard emacs modeˇ
  638.  
  639. To use Emacs commands on small keyboards without a Ícontrolˇ key, the menu item "Small
  640. Keyboard" may be checked in the Edit menu. When it is checked, all keyboard events are
  641. interpreted as follows:
  642.  
  643. • the Íoptionˇ key acts as the Ícontrolˇ key
  644.  
  645. • pressing the Íoptionˇ and Ícommandˇ keys simultaneously acts the same as the
  646. Ícontrol-optionˇ combination.
  647.  
  648. The original keyboard interpretation is restored by unchecking Small Keyboard mode.
  649.  
  650. Note that Small Keyboard mode has no effect on other events (e.g., mouse modifiers remain
  651. as documented earlier).
  652.  
  653. There is one quirk in small keyboard mode: since Íoption-nˇ and Íoption-eˇ are normally
  654. "dead keys," MacGambit requires a special keyboard mapping resource (KMAP). For System
  655. 6 users, nothing special needs to be done - the KMAP resource is included in the MacGambit
  656. application. For System 7 users, you must drag the file "U.S. Live" onto your System file for
  657. the MacGambit keyboard mapping to take effect. MacGambit's keyboard mapping will only
  658. affect MacGambit, the dead keys will continue to work in other applications.
  659.  
  660.  
  661. ÛC.7. Mouseˇ
  662.  
  663. The editing action is specified by the number of mouse button clicks:
  664.  
  665. Í    single -ˇ position insertion point
  666. Í    double -ˇ select word
  667. Í    triple -ˇ select line
  668.  
  669. Dragging after one of these clicks extends the selection by character, word, or line
  670. respectively; the anchor point is the original click position, the active end is the other end of
  671. the selection. After a click-drag, the active end can be moved with Íshiftˇ clicks or Íshiftˇ
  672. arrow keys. The anchor and active ends of the selection can be swapped with
  673. Íoption+shift+clickˇ, or Íoption+shift+commandˇ up/down or left/right arrow keys.
  674.  
  675.  
  676. ÛC.8. Scrollingˇ
  677.  
  678. Holding down Ícommandˇ, Íshiftˇ, Íoptionˇ, and Ícontrolˇ keys speeds up scrolling when
  679. mousing in the arrows of the scroll bar. The more keys you hold down the faster it scrolls
  680. (exponentially).
  681.  
  682.  
  683. ÛC.9. Editor limitationsˇ
  684.  
  685. The text size is limited by memory space. There is a limit of 32,000 lines (not characters)
  686. per window. The editor has been used with files several hundred kilobytes in size.
  687.  
  688.  
  689. ÛD. Language extensionsˇ
  690.  
  691. This chapter gives a list of the procedures and special forms supported by MacGambit that
  692. are not in R4RS.
  693.  
  694.  
  695. ÛD.1. Concurrent evaluationˇ
  696.  
  697.  
  698. Í(future˘ ˇ<expression>Í)                                             ˇsyntax
  699.  
  700. The Ífutureˇ construct is used to implement ˘concurrent evaluationˇ. Í(future ˇ<expression>Í)ˇ
  701. creates a task to evaluate <expression> concurrently with the rest of the program (the
  702. continuation). The value returned by Ífutureˇ represents the value eventually computed for
  703. <expression>. If a task tries to use the value in a strict operation (such as Í+ˇ and Ícarˇ, but not
  704. Íconsˇ), the task is suspended until the value is known.
  705.  
  706. Í    (define (f n) (expt 3 n))
  707. Í    
  708. Í    (let ((x (future (f 101))))
  709. Í      (let ((y (f 100)))
  710. Í        (/ x y)))               ==>  3
  711. Í    
  712. Í    (let ((a 1))
  713. Í      (future
  714. Í        (set! a (/ (f 101) (f 100))))
  715. Í      a)                        ==>  1 ˘orÍ 3
  716. Í    
  717. Í    (define count 0)
  718. Í    (define (inc) (set! count (+ count 1)))
  719. Í    (define x (future (let loop () (inc) (loop))))
  720. Í    count                       ==>  1014
  721. Í    count                       ==>  3055 ˘etc.ˇ
  722.  
  723.  
  724. Í(touch˘ objÍ)                                                   ˇprocedure
  725.  
  726. ÍTouchˇ is a strict operation that returns its argument.
  727.  
  728. Í    (touch 5)                   ==>  5
  729. Í    (touch (future 5))          ==>  5ˇ
  730.  
  731.  
  732. ÛD.2. Semaphoresˇ
  733.  
  734.  
  735. Í(make-semaphore)                                             ˇprocedure
  736. Í(semaphore-wait˘ semaphoreÍ)                                     ˇprocedure
  737. Í(semaphore-signal˘ semaphoreÍ)                                   ˇprocedure
  738.  
  739. These procedures implement the semaphore data type. Semaphores are generally used to
  740. enforce mutual exclusion. ÍSemaphore-waitˇ locks the semaphore and Ísemaphore-signalˇ
  741. unlocks it. A task trying to lock a semaphore with Ísemaphore-waitˇ is suspended until the
  742. semaphore is unlocked.
  743.  
  744.  
  745. ÛD.3. Queuesˇ
  746.  
  747.  
  748. Í(make-queue)                                                 ˇprocedure
  749. Í(queue-put!˘ queue objÍ)                                          ˇprocedure
  750. Í(queue-get!˘ queueÍ)                                            ˇprocedure
  751.  
  752. These procedures implement the queue data type. A queue is a sequence of items. Items are
  753. added to the tail of the sequence by Íqueue-put!ˇ. ÍQueue-get!ˇ removes the next element at
  754. the head of the sequence. If the sequence is empty, Íqueue-get!ˇ returns Í#fˇ. Otherwise a
  755. single element list containing the item is returned. ÍQueue-put!ˇ and Íqueue-get!ˇ are
  756. atomic operations.
  757.  
  758. Í    (define q (make-queue))
  759. Í    (queue-put! q 1)
  760. Í    (queue-put! q 2)
  761. Í    (queue-get! q)              ==>  (1)
  762. Í    (queue-get! q)              ==>  (2)
  763. Í    (queue-get! q)              ==>  #fˇ
  764.  
  765.  
  766. ÛD.4. Weak Pairsˇ
  767.  
  768.  
  769. Í(weak-pair?˘ objÍ)                                              ˇprocedure
  770. Í(weak-cons˘ obj¯1˘ obj¯2Í)                                          ˇprocedure
  771. Í(weak-car˘ pairÍ)                                                ˇprocedure
  772. Í(weak-cdr˘ pairÍ)                                                ˇprocedure
  773. Í(weak-set-car!˘ pair objÍ)                                        ˇprocedure
  774. Í(weak-set-cdr!˘ pair objÍ)                                        ˇprocedure
  775.  
  776. These procedures are the corresponding pair operations for the "weak pair" data type. Weak
  777. pairs are similar to normal pairs except that the object in the car field is "weakly linked" as
  778. far as garbage collection is concerned. The car field will be set to Í#fˇ by the garbage
  779. collector if it finds that there are only weak links to the object.
  780.  
  781.  
  782. ÛD.5. Portsˇ
  783.  
  784.  
  785. Í(open-input-string˘ stringÍ)                                     ˇprocedure
  786. Í(open-output-string)                                         ˇprocedure
  787. Í(get-output-string˘ portÍ)                                      ˇprocedure
  788.  
  789. These procedures implement string ports. String ports can be used like normal ports. The
  790. characters are obtained from (or sent to) a string instead of a file. An output string port
  791. accumulates the characters written to the port. The procedure Íget-output-stringˇ
  792. extracts the sequence of characters accumulated up to that point and returns them as a
  793. string.
  794.  
  795. Í    (define ip (open-input-string "(1 2)foo"))
  796. Í    (define op (open-output-string))
  797. Í    (read ip)                   ==>  (1 2)
  798. Í    (read ip)                   ==>  foo
  799. Í    (read ip)                   ==>  #[eof]
  800. Í    (write 'bar op)
  801. Í    (write '(a b) op)
  802. Í    (get-output-string op)      ==>  "bar(a b)"ˇ
  803.  
  804.  
  805. Í(with-input-from-string˘ string thunkÍ)                            ˇprocedure
  806. Í(with-output-to-string˘ thunkÍ)                                  ˇprocedure
  807.  
  808. The procedure Íwith-input-from-stringˇ is similar to Íwith-input-from-fileˇ except
  809. that the characters are obtained from a string. The procedure Íwith-output-to-stringˇ
  810. calls the thunk and returns a string containing all characters output to the current output
  811. port.
  812.  
  813. Í    (with-input-from-string
  814. Í      "a string"
  815. Í      (lambda ()
  816. Í        (read-char)))           ==> #\a
  817. Í    
  818. Í    (with-output-to-string
  819. Í      (lambda ()
  820. Í        (write (cons 1 2))))    ==> "(1 . 2)"ˇ
  821.  
  822.  
  823. Í(with-input-from-port˘ port thunkÍ)                               ˇprocedure
  824. Í(with-output-to-port˘ port thunkÍ)                                ˇprocedure
  825.  
  826. These procedures are respectively similar to Íwith-input-from-fileˇ and
  827. Íwith-output-to-fileˇ. The difference is that the first argument is a port instead of a file
  828. name.
  829.  
  830.  
  831. Í(pretty-print˘ objÍ)                                            ˇprocedure
  832. Í(pretty-print˘ obj portÍ)                                         ˇprocedure
  833. Í(pretty-print˘ obj port kÍ)                                       ˇprocedure
  834. Í(pp˘ objÍ)                                                      ˇprocedure
  835. Í(pp˘ obj portÍ)                                                   ˇprocedure
  836. Í(pp˘ obj port kÍ)                                                 ˇprocedure
  837.  
  838. ÍPretty-printˇ and Íppˇ are similar to Íwriteˇ except that the result is nicely formatted. The
  839. argument ˘kˇ specifies the width of the page. If ˘objˇ is a procedure created by the interpreter,
  840. Íppˇ will display its definition.
  841.  
  842.  
  843. ÛD.6. Symbolsˇ
  844.  
  845.  
  846. Í(gensym)                                                     ˇprocedure
  847. Í(gensym˘ prefixÍ)                                                ˇprocedure
  848.  
  849. ÍGensymˇ returns a new "uninterned" symbol. ˘Prefixˇ is used as a prefix to the generated
  850. name. ˘Prefixˇ is either a string or symbol and it defaults to "g".
  851.  
  852.  
  853. Í(get˘ symbol propÍ)                                              ˇprocedure
  854. Í(put˘ symbol prop objÍ)                                            ˇprocedure
  855.  
  856. ÍGetˇ and Íputˇ access the property list of symbols. ÍPutˇ associates the value ˘objˇ with the
  857. property ˘propˇ of the symbol. ÍGetˇ extracts the value associated with the property ˘propˇ of
  858. the symbol. ÍGetˇ returns Í#fˇ if ˘propˇ has not been set for the symbol. ˘Propˇ can be any object.
  859.  
  860.  
  861. ÛD.7. Evaluationˇ
  862.  
  863.  
  864. Í(eval˘ objÍ)                                                    ˇprocedure
  865.  
  866. ÍEvalˇ's argument is a datum representing an expression. ÍEvalˇ evaluates this expression in
  867. the global environment and returns the result.
  868.  
  869. Í    (eval '(+ 1 2))             ==>  3
  870. Í    (eval '(define x 1))        ==>  ˘unspecifiedÍ
  871. Í    x                           ==>  1ˇ
  872.  
  873.  
  874. Í(compile-file˘ filename option ...Í)                                 ˇprocedure
  875.  
  876. ˘Filenameˇ should be a string naming an existing file containing Scheme source code. Any of the
  877. compilation options described below can be specified after the filename. ÍCompile-fileˇ
  878. compiles the source file into an object file which can be loaded with the Íloadˇ procedure or
  879. linked into a standalone application by the linker. By convention, source files have a ".scm"
  880. extension. Object files have a ".O" extension.
  881.  
  882. The following options are accepted by the compiler:
  883.  
  884. Íverboseˇ -- Generates a trace of the compiler's actions.
  885.  
  886. Íreportˇ -- Produces a global variable usage report. Each global variable used in the program
  887. is listed with 4 flags that indicate if the global variable is defined, referenced, mutated and
  888. called.
  889.  
  890. Íexpansionˇ -- Shows the source code after macro expansion.
  891.  
  892. Ípvmˇ -- Produces a listing of the PVM code for the program on "<file>.pvm".
  893.  
  894. Íasmˇ -- Produces the assembly language output file "<file>.asm".
  895.  
  896. Ídebugˇ -- Generates code that includes information to aid debugging.
  897.  
  898. Note: The compiler is only available in the full version of MacGambit.
  899.  
  900.  
  901. ÛD.8. Tracingˇ
  902.  
  903.  
  904. Í(trace˘ ˇ<name>˘ ...Í)                                                ˇsyntax
  905. Í(untrace˘ ˇ<name>˘ ...Í)                                              ˇsyntax
  906.  
  907. ÍTraceˇ starts tracing calls to the specified procedures. ÍUntraceˇ stops the tracing. The form
  908. Í(trace)ˇ returns the names of the currently traced procedures and Í(untrace)ˇ stops the
  909. tracing on all those procedures.
  910.  
  911.  
  912. ÛD.9. Miscellaneousˇ
  913.  
  914.  
  915. Í(error˘ string obj ...Í)                                            ˇprocedure
  916.  
  917. ÍErrorˇ signals an error and causes a new read-eval-print loop to be entered. The message
  918. displayed is Ístringˇ followed by the remaining arguments. The continuation of the
  919. read-eval-print loop is the same as the one passed to Íerrorˇ. Thus, returning from the
  920. read-eval-print loop causes a return from the call to Íerrorˇ.
  921.  
  922. Í    (define (real-sqrt x)
  923. Í      (if (< x 0)
  924. Í        (error "bad value to real-sqrt:" x)
  925. Í        (sqrt x)))ˇ
  926.  
  927.  
  928. Í(exit)                                                       ˇprocedure
  929.  
  930. ÍExitˇ causes the program to terminate.
  931.  
  932.  
  933. Í(runtime)                                                    ˇprocedure
  934.  
  935. ÍRuntimeˇ returns the number of seconds since the program was started.
  936.  
  937.  
  938. Í(set-gc-report˘ booleanÍ)                                        ˇprocedure
  939.  
  940. ÍSet-gc-reportˇ controls the generation of reports on garbage collections. If the argument
  941. is true, a brief report of memory usage is generated on every garbage collection.
  942.  
  943.  
  944. ÛD.10. Graphicsˇ
  945.  
  946.  
  947. Í(clear-graphics)                                             ˇprocedure
  948. Í(position-pen˘ x yÍ)                                            ˇprocedure
  949. Í(draw-line-to˘ x yÍ)                                            ˇprocedure
  950. Í(draw-point˘ x yÍ)                                              ˇprocedure
  951. Í(clear-point˘ x yÍ)                                             ˇprocedure
  952. Í(graphics-text˘ string x yÍ)                                      ˇprocedure
  953.  
  954. MacGambit has a "drawing" window which supports simple graphics. This window is initially
  955. hidden but it pops up when any of the above procedures are called. The coordinates of the
  956. drawing window go from -200 to +200 in both the x (horizontal) and y (vertical) axis. The
  957. window is scaled by a factor of 1/2 (i.e. a distance of 10 units corresponds to 5 pixels on
  958. the screen). The origin is in the center of the window. Lines are drawn using a "pen".
  959. ÍPosition-penˇ sets the position of the pen and calls to Ídraw-line-toˇ move the pen to a
  960. new coordinate leaving a line behind. The other procedures do not move the pen.
  961.  
  962.  
  963. ÛD.11. Text windowsˇ
  964.  
  965.  
  966. Í(open-text-window˘ stringÍ)                                      ˇprocedure
  967.  
  968. This procedure opens an editable text window with the given title and returns a port to
  969. access the window. The port is both an input and output port. Writing to the port displays
  970. text in the window. Reading from the port returns the expression (or text) that has been
  971. typed in this window and "sent" with Ícmd-returnˇ (or Íenterˇ for text). Closing the port
  972. closes the window.
  973.  
  974.  
  975. ÛD.12. Toolbox interfaceˇ
  976.  
  977. Some of the most common Macintosh Toolbox routines are available as Scheme procedures.
  978. BEWARE: these procedures do not check the validity of their arguments so it is easy to crash
  979. the system by passing an illegal value.
  980.  
  981. In general the procedures take the same parameters as the corresponding definition in "Inside
  982. Macintosh". The notable exception is for Quickdraw procedures. Instead of operating on the
  983. "current grafport", the quickdraw procedures take an extra argument that specifies the port
  984. to use. This is always the first parameter.
  985.  
  986. Here is a list of the procedures available:
  987.  
  988. Í; Windows
  989. Í
  990. Í(mac#newwindow bounds title visible procid behind goaway)
  991. Í(mac#getnewwindow windowid behind)
  992. Í(mac#disposewindow w)
  993. Í(mac#selectwindow w)
  994. Í(mac#hidewindow w)
  995. Í(mac#showwindow w)
  996. Í(mac#frontwindow)
  997. Í(mac#findwindow pt w-cell) ; w-cell is a pair, the car is the result
  998. Í(mac#trackgoaway w pt)
  999. Í(mac#dragwindow w pt r)
  1000. Í(mac#invalrect port r)
  1001. Í(mac#beginupdate w)
  1002. Í(mac#endupdate w)
  1003. Í
  1004. Í; Quickdraw
  1005. Í
  1006. Í(mac#point v h)
  1007. Í(mac#point-v r)
  1008. Í(mac#point-h r)
  1009. Í(mac#point-v-set! r x)
  1010. Í(mac#point-h-set! r x)
  1011. Í
  1012. Í(mac#rect top left bottom right)
  1013. Í(mac#rect-top r)
  1014. Í(mac#rect-left r)
  1015. Í(mac#rect-bottom r)
  1016. Í(mac#rect-right r)
  1017. Í(mac#rect-top-set! r x)
  1018. Í(mac#rect-left-set! r x)
  1019. Í(mac#rect-bottom-set! r x)
  1020. Í(mac#rect-right-set! r x)
  1021. Í
  1022. Í(mac#openport port)
  1023. Í(mac#initport port)
  1024. Í(mac#closeport port)
  1025. Í(mac#setport port)
  1026. Í(mac#getport)
  1027. Í(mac#setorigin port h v)
  1028. Í(mac#backpat port pat)
  1029. Í(mac#hidecursor)
  1030. Í(mac#showcursor)
  1031. Í(mac#pensize port width height)
  1032. Í(mac#penmode port mode)
  1033. Í(mac#penpat port pat)
  1034. Í(mac#pennormal port)
  1035. Í(mac#moveto port h v)
  1036. Í(mac#move port dh dv)
  1037. Í(mac#lineto port h v)
  1038. Í(mac#line port dh dv)
  1039. Í(mac#textfont port font)
  1040. Í(mac#textface port face)
  1041. Í(mac#textmode port mode)
  1042. Í(mac#textsize port size)
  1043. Í(mac#spaceextra port extra)
  1044. Í(mac#drawchar port ch)
  1045. Í(mac#drawstring port s)
  1046. Í(mac#drawtext port textbuf firstbyte bytecount) ; textbuf is a string
  1047. Í(mac#charwidth port ch)
  1048. Í(mac#stringwidth port s)
  1049. Í(mac#textwidth port textbuf firstbyte bytecount) ; textbuf is a string
  1050. Í(mac#localtoglobal port pt)
  1051. Í(mac#globaltolocal port pt)
  1052. Í(mac#framerect port r)
  1053. Í(mac#paintrect port r)
  1054. Í(mac#eraserect port r)
  1055. Í(mac#invertrect port r)
  1056. Í(mac#fillrect port r pat)
  1057. Í(mac#frameroundrect port r ovwd ovht)
  1058. Í(mac#paintroundrect port r ovwd ovht)
  1059. Í(mac#eraseroundrect port r ovwd ovht)
  1060. Í(mac#invertroundrect port r ovwd ovht)
  1061. Í(mac#fillroundrect port r ovwd ovht pat)
  1062. Í(mac#frameoval port r)
  1063. Í(mac#paintoval port r)
  1064. Í(mac#eraseoval port r)
  1065. Í(mac#invertoval port r)
  1066. Í(mac#filloval port r pat)
  1067. Í(mac#framearc port r startangle arcangle)
  1068. Í(mac#paintarc port r startangle arcangle)
  1069. Í(mac#erasearc port r startangle arcangle)
  1070. Í(mac#invertarc port r startangle arcangle)
  1071. Í(mac#fillarc port r startangle arcangle pat)
  1072. Í
  1073. Í; Menus
  1074. Í
  1075. Í(mac#newmenu menuid str)
  1076. Í(mac#getmenu resourceid)
  1077. Í(mac#disposemenu themenu)
  1078. Í(mac#appendmenu themenu str)
  1079. Í(mac#addresmenu themenu thetype)
  1080. Í(mac#insertresmenu themenu thetype afteritem)
  1081. Í(mac#insertmenu themenu beforeid)
  1082. Í(mac#drawmenubar)
  1083. Í(mac#deletemenu menuid)
  1084. Í(mac#clearmenubar)
  1085. Í(mac#getnewmbar menubarid)
  1086. Í(mac#getmenubar)
  1087. Í(mac#setmenubar menulist)
  1088. Í(mac#menuselect p)
  1089. Í(mac#menukey ch)
  1090. Í(mac#hilitemenu menuid)
  1091. Í(mac#disableitem themenu item)
  1092. Í(mac#enableitem themenu item)
  1093. Í(mac#getmhandle menuid)
  1094. Í
  1095. Í; Events
  1096. Í
  1097. Í(mac#event-what event)
  1098. Í(mac#event-message event)
  1099. Í(mac#event-when event)
  1100. Í(mac#event-where event)
  1101. Í(mac#event-modifiers event)
  1102. Í
  1103. Í(mac#modifiers-button? modifiers)
  1104. Í(mac#modifiers-command? modifiers)
  1105. Í(mac#modifiers-shift? modifiers)
  1106. Í(mac#modifiers-alphalock? modifiers)
  1107. Í(mac#modifiers-option? modifiers)
  1108. Í
  1109. Í; Standard file get/put
  1110. Í
  1111. Í(mac#sfgetfile) ; returns a filename or #f
  1112. Í(mac#sfputfile) ; returns a filename or #f
  1113. Í
  1114. Í; Other procedures
  1115. Í
  1116. Í(mac#getmouse pt)
  1117. Í(mac#button)
  1118. Í(mac#tickcount)
  1119. Í(mac#delay duration)
  1120. Í(mac#sysbeep duration)
  1121. Í(mac#seteventmask themask)
  1122. ͡
  1123.  
  1124.  
  1125. Û1. Overview of Schemeˇ
  1126.  
  1127.  
  1128. Û1.1. Semanticsˇ
  1129.  
  1130. This section gives an overview of Scheme's semantics. A detailed informal semantics is the
  1131. subject of chapters 3 through 6. For reference purposes, section 7.2 provides a formal
  1132. semantics of Scheme.
  1133.  
  1134. Following Algol, Scheme is a statically scoped programming language. Each use of a variable
  1135. is associated with a lexically apparent binding of that variable.
  1136.  
  1137. Scheme has latent as opposed to manifest types. Types are associated with values (also
  1138. called objects) rather than with variables. (Some authors refer to languages with latent
  1139. types as weakly typed or dynamically typed languages.) Other languages with latent types
  1140. are APL, Snobol, and other dialects of Lisp. Languages with manifest types (sometimes
  1141. referred to as strongly typed or statically typed languages) include Algol 60, Pascal, and C.
  1142.  
  1143. All objects created in the course of a Scheme computation, including procedures and
  1144. continuations, have unlimited extent. No Scheme object is ever destroyed. The reason that
  1145. implementations of Scheme do not (usually!) run out of storage is that they are permitted to
  1146. reclaim the storage occupied by an object if they can prove that the object cannot possibly
  1147. matter to any future computation. Other languages in which most objects have unlimited
  1148. extent include APL and other Lisp dialects.
  1149.  
  1150. Implementations of Scheme are required to be properly tail-recursive. This allows the
  1151. execution of an iterative computation in constant space, even if the iterative computation is
  1152. described by a syntactically recursive procedure. Thus with a tail-recursive
  1153. implementation, iteration can be expressed using the ordinary procedure-call mechanics, so
  1154. that special iteration constructs are useful only as syntactic sugar.
  1155.  
  1156. Scheme procedures are objects in their own right. Procedures can be created dynamically,
  1157. stored in data structures, returned as results of procedures, and so on. Other languages with
  1158. these properties include Common Lisp and ML.
  1159.  
  1160. One distinguishing feature of Scheme is that continuations, which in most other languages
  1161. only operate behind the scenes, also have ``first-class'' status. Continuations are useful for
  1162. implementing a wide variety of advanced control constructs, including non-local exits,
  1163. backtracking, and coroutines. See section 6.9.
  1164.  
  1165. Arguments to Scheme procedures are always passed by value, which means that the actual
  1166. argument expressions are evaluated before the procedure gains control, whether the
  1167. procedure needs the result of the evaluation or not. ML, C, and APL are three other
  1168. languages that always pass arguments by value. This is distinct from the lazy-evaluation
  1169. semantics of Haskell, or the call-by-name semantics of Algol 60, where an argument
  1170. expression is not evaluated unless its value is needed by the procedure.
  1171.  
  1172. Scheme's model of arithmetic is designed to remain as independent as possible of the
  1173. particular ways in which numbers are represented within a computer. In Scheme, every
  1174. integer is a rational number, every rational is a real, and every real is a complex number.
  1175. Thus the distinction between integer and real arithmetic, so important to many programming
  1176. languages, does not appear in Scheme. In its place is a distinction between exact arithmetic,
  1177. which corresponds to the mathematical ideal, and inexact arithmetic on approximations. As
  1178. in Common Lisp, exact arithmetic is not limited to integers.
  1179.  
  1180.  
  1181. Û1.2. Syntaxˇ
  1182.  
  1183. Scheme, like most dialects of Lisp, employs a fully parenthesized prefix notation for
  1184. programs and (other) data; the grammar of Scheme generates a sublanguage of the language
  1185. used for data. An important consequence of this simple, uniform representation is the
  1186. susceptibility of Scheme programs and data to uniform treatment by other Scheme
  1187. programs.
  1188.  
  1189. The Íreadˇ procedure performs syntactic as well as lexical decomposition of the data it reads.
  1190. The Íreadˇ procedure parses its input as data (section 7.1.2), not as program.
  1191.  
  1192. The formal syntax of Scheme is described in section 7.1.
  1193.  
  1194.  
  1195. Û1.3. Notation and terminologyˇ
  1196.  
  1197.  
  1198. ¸1.3.1. Essential and non-essential featuresˇ
  1199.  
  1200. It is required that every implementation of Scheme support features that are marked as
  1201. being ˘essentialˇ. Features not explicitly marked as essential are not essential.
  1202. Implementations are free to omit non-essential features of Scheme or to add extensions,
  1203. provided the extensions are not in conflict with the language reported here. In particular,
  1204. implementations must support portable code by providing a syntactic mode that preempts no
  1205. lexical conventions of this report and reserves no identifiers other than those listed as
  1206. syntactic keywords in section 2.1.
  1207.  
  1208.  
  1209. ¸1.3.2. Error situations and unspecified behaviorˇ
  1210.  
  1211. When speaking of an error situation, this report uses the phrase ``an error is signalled'' to
  1212. indicate that implementations must detect and report the error. If such wording does not
  1213. appear in the discussion of an error, then implementations are not required to detect or
  1214. report the error, though they are encouraged to do so. An error situation that
  1215. implementations are not required to detect is usually referred to simply as ``an error.''
  1216.  
  1217. For example, it is an error for a procedure to be passed an argument that the procedure is
  1218. not explicitly specified to handle, even though such domain errors are seldom mentioned in
  1219. this report. Implementations may extend a procedure's domain of definition to include such
  1220. arguments.
  1221.  
  1222. This report uses the phrase ``may report a violation of an implementation restriction'' to
  1223. indicate circumstances under which an implementation is permitted to report that it is unable
  1224. to continue execution of a correct program because of some restriction imposed by the
  1225. implementation. Implementation restrictions are of course discouraged, but implementations
  1226. are encouraged to report violations of implementation restrictions.
  1227.  
  1228. For example, an implementation may report a violation of an implementation restriction if it
  1229. does not have enough storage to run a program.
  1230.  
  1231. If the value of an expression is said to be ``unspecified,'' then the expression must evaluate
  1232. to some object without signalling an error, but the value depends on the implementation; this
  1233. report explicitly does not say what value should be returned.
  1234.  
  1235.  
  1236. ¸1.3.3. Entry formatˇ
  1237.  
  1238. Chapters 4 and 6 are organized into entries. Each entry describes one language feature or a
  1239. group of related features, where a feature is either a syntactic construct or a built-in
  1240. procedure. An entry begins with one or more header lines of the form
  1241.  
  1242. ˘templateÍ                                                ˇessential ˘categoryˇ
  1243.  
  1244. if the feature is an essential feature, or simply
  1245.  
  1246. ˘templateÍ                                                      ˇ ˘categoryˇ
  1247.  
  1248. if the feature is not an essential feature.
  1249.  
  1250. If ˘categoryˇ is ``syntax'', the entry describes an expression type, and the header line gives
  1251. the syntax of the expression type. Components of expressions are designated by syntactic
  1252. variables, which are written using angle brackets, for example, <expression>, <variable>.
  1253. Syntactic variables should be understood to denote segments of program text; for example,
  1254. <expression> stands for any string of characters which is a syntactically valid expression.
  1255. The notation
  1256.  
  1257.     <thing˛1ˇ> ...
  1258.  
  1259. indicates zero or more occurrences of a <thing>, and
  1260.  
  1261.     <thing˛1ˇ> <thing˛2ˇ> ...
  1262.  
  1263. indicates one or more occurrences of a <thing>.
  1264.  
  1265. If ˘categoryˇ is ``procedure'', then the entry describes a procedure, and the header line gives a
  1266. template for a call to the procedure. Argument names in the template are ˘italicizedˇ. Thus the
  1267. header line
  1268.  
  1269. Í(vector-ref ˘vectorÍ ˘kÍ)                                  ˇessential procedure
  1270.  
  1271. indicates that the essential built-in procedure Ívector-refˇ takes two arguments, a vector
  1272. ˘vectorˇ and an exact non-negative integer ˘kˇ (see below). The header lines 
  1273. Í(make-vector ˘kÍ)                                       ˇessential procedure
  1274. Í(make-vector ˘kÍ ˘fillÍ)                                           ˇprocedure
  1275.  
  1276. indicate that in all implementations, the Ímake-vectorˇ procedure must be defined to take one
  1277. argument, and some implementations will extend it to take two arguments.
  1278.  
  1279. It is an error for an operation to be presented with an argument that it is not specified to
  1280. handle. For succinctness, we follow the convention that if an argument name is also the name
  1281. of a type listed in section 3.4, then that argument must be of the named type. For example,
  1282. the header line for Ívector-refˇ given above dictates that the first argument to
  1283. Ívector-refˇ must be a vector. The following naming conventions also imply type
  1284. restrictions:
  1285.  
  1286.     
  1287.     ˘objÍ                              ˇany object
  1288.     ˘listˇ, ˘list¯1ˇ, ... ˘list¯jˇ, ...Í                ˇlist (see section 6.3)
  1289.     ˘zˇ, ˘z¯1ˇ, ... ˘z¯jˇ, ...Í                     ˇcomplex number
  1290.     ˘xˇ, ˘x¯1ˇ, ... ˘x¯jˇ, ...Í                     ˇreal number
  1291.     ˘yˇ, ˘y¯1ˇ, ... ˘y¯jˇ, ...Í                     ˇreal number
  1292.     ˘qˇ, ˘q¯1ˇ, ... ˘q¯jˇ, ...Í                     ˇrational number
  1293.     ˘nˇ, ˘n¯1ˇ, ... ˘n¯jˇ, ...Í                     ˇinteger
  1294.     ˘kˇ, ˘k¯1ˇ, ... ˘k¯jˇ, ...Í                     ˇexact non-negative integer
  1295.  
  1296.  
  1297.  
  1298. ¸1.3.4. Evaluation examplesˇ
  1299.  
  1300. The symbol ``Í==>ˇ'' used in program examples should be read ``evaluates to.'' For example,
  1301.  
  1302. Í    (* 5 8)                     ==>  40ˇ
  1303.  
  1304. means that the expression Í(* 5 8)ˇ evaluates to the object Í40ˇ. Or, more precisely: the
  1305. expression given by the sequence of characters ``Í(* 5 8)ˇ'' evaluates, in the initial
  1306. environment, to an object that may be represented externally by the sequence of characters
  1307. ``Í40ˇ''. See section 3.3 for a discussion of external representations of objects.
  1308.  
  1309.  
  1310. ¸1.3.5. Naming conventionsˇ
  1311.  
  1312. By convention, the names of procedures that always return a boolean value usually end in
  1313. ``Í?ˇ''. Such procedures are called predicates.
  1314.  
  1315. By convention, the names of procedures that store values into previously allocated locations
  1316. (see section 3.5) usually end in ``Í!ˇ''. Such procedures are called mutation procedures. By
  1317. convention, the value returned by a mutation procedure is unspecified.
  1318.  
  1319. By convention, ``Í->ˇ'' appears within the names of procedures that take an object of one type
  1320. and return an analogous object of another type. For example, Ílist->vectorˇ takes a list and
  1321. returns a vector whose elements are the same as those of the list.
  1322.  
  1323.  
  1324. Û2. Lexical conventionsˇ
  1325.  
  1326. This section gives an informal account of some of the lexical conventions used in writing
  1327. Scheme programs. For a formal syntax of Scheme, see section 7.1.
  1328.  
  1329. Upper and lower case forms of a letter are never distinguished except within character and
  1330. string constants. For example, ÍFooˇ is the same identifier as ÍFOOˇ, and Í#x1ABˇ is the same
  1331. number as Í#X1abˇ.
  1332.  
  1333.  
  1334. Û2.1. Identifiersˇ
  1335.  
  1336. Most identifiers allowed by other programming languages are also acceptable to Scheme. The
  1337. precise rules for forming identifiers vary among implementations of Scheme, but in all
  1338. implementations a sequence of letters, digits, and ``extended alphabetic characters'' that
  1339. begins with a character that cannot begin a number is an identifier. In addition, Í+ˇ, Í-ˇ, and Í...ˇ
  1340. are identifiers. Here are some examples of identifiers:
  1341.  
  1342. Í    lambda                   q
  1343. Í    list->vector             soup
  1344. Í    +                        V17a
  1345. Í    <=?                      a34kTMNs
  1346. Í    the-word-recursion-has-many-meaningsˇ
  1347.  
  1348. Extended alphabetic characters may be used within identifiers as if they were letters. The
  1349. following are extended alphabetic characters:
  1350.  
  1351. Í    + - . * / < = > ! ? : $ % _ & ~ ^ˇ
  1352.  
  1353. See section 7.1.1 for a formal syntax of identifiers.
  1354.  
  1355. Identifiers have several uses within Scheme programs:
  1356.  
  1357. • Certain identifiers are reserved for use as syntactic keywords (see below).
  1358.  
  1359. • Any identifier that is not a syntactic keyword may be used as a variable (see section 3.1).
  1360.  
  1361. • When an identifier appears as a literal or within a literal (see section 4.1.2), it is being
  1362. used to denote a ˘symbolˇ (see section 6.4).
  1363.  
  1364. The following identifiers are syntactic keywords, and should not be used as variables:
  1365.  
  1366. Í    =>           do            or
  1367. Í    and          else          quasiquote
  1368. Í    begin        if            quote
  1369. Í    case         lambda        set!
  1370. Í    cond         let           unquote
  1371. Í    define       let*          unquote-splicing
  1372. Í    delay        letrecˇ
  1373.  
  1374. Some implementations allow all identifiers, including syntactic keywords, to be used as
  1375. variables. This is a compatible extension to the language, but ambiguities in the language
  1376. result when the restriction is relaxed, and the ways in which these ambiguities are resolved
  1377. vary between implementations.
  1378.  
  1379.  
  1380. Û2.2. Whitespace and commentsˇ
  1381.  
  1382. ˘Whitespaceˇ characters are spaces and newlines. (Implementations typically provide
  1383. additional whitespace characters such as tab or page break.) Whitespace is used for
  1384. improved readability and as necessary to separate tokens from each other, a token being an
  1385. indivisible lexical unit such as an identifier or number, but is otherwise insignificant.
  1386. Whitespace may occur between any two tokens, but not within a token. Whitespace may also
  1387. occur inside a string, where it is significant.
  1388.  
  1389. A semicolon (Í;ˇ) indicates the start of a comment. The comment continues to the end of the
  1390. line on which the semicolon appears. Comments are invisible to Scheme, but the end of the
  1391. line is visible as whitespace. This prevents a comment from appearing in the middle of an
  1392. identifier or number.
  1393.  
  1394. Í    ;;; The FACT procedure computes the factorial
  1395. Í    ;;; of a non-negative integer.
  1396. Í    (define fact
  1397. Í      (lambda (n)
  1398. Í        (if (= n 0)
  1399. Í            1        ;Base case: return 1
  1400. Í            (* n (fact (- n 1))))))ˇ
  1401.  
  1402.  
  1403. Û2.3. Other notationsˇ
  1404.  
  1405. For a description of the notations used for numbers, see section 6.5.
  1406.  
  1407. Í. + -ˇ These are used in numbers, and may also occur anywhere in an identifier except as
  1408. the first character. A delimited plus or minus sign by itself is also an identifier. A delimited
  1409. period (not occurring within a number or identifier) is used in the notation for pairs (section
  1410. 6.3), and to indicate a rest-parameter in a formal parameter list (section 4.1.4). A delimited
  1411. sequence of three successive periods is also an identifier.
  1412.  
  1413. Í( )ˇ Parentheses are used for grouping and to notate lists (section 6.3).
  1414.  
  1415. Í'ˇ The single quote character is used to indicate literal data (section 4.1.2).
  1416.  
  1417. Í`ˇ The backquote character is used to indicate almost-constant data (section 4.2.6).
  1418.  
  1419. Í, ,@ˇ The character comma and the sequence comma at-sign are used in conjunction with
  1420. backquote (section 4.2.6).
  1421.  
  1422. Í"ˇ The double quote character is used to delimit strings (section 6.7).
  1423.  
  1424. Í\ˇ Backslash is used in the syntax for character constants (section 6.6) and as an escape
  1425. character within string constants (section 6.7).
  1426.  
  1427. Í[ ] { }ˇ Left and right square brackets and curly braces are reserved for possible future
  1428. extensions to the language.
  1429.  
  1430. Í#ˇ Sharp sign is used for a variety of purposes depending on the character that immediately
  1431. follows it:
  1432.  
  1433. Í#tˇ Í#fˇ These are the boolean constants (section 6.1).
  1434.  
  1435. Í#\ˇ This introduces a character constant (section 6.6).
  1436.  
  1437. Í#(ˇ This introduces a vector constant (section 6.8). Vector constants are terminated by Í)ˇ .
  1438.  
  1439. Í#e #i #b #o #d #xˇ These are used in the notation for numbers (section 6.5.4).
  1440.  
  1441.  
  1442. Û3. Basic conceptsˇ
  1443.  
  1444.  
  1445. Û3.1. Variables and regionsˇ
  1446.  
  1447. Any identifier that is not a syntactic keyword (see section 2.1) may be used as a variable. A
  1448. variable may name a location where a value can be stored. A variable that does so is said to
  1449. be ˘boundˇ to the location. The set of all visible bindings in effect at some point in a program is
  1450. known as the ˘environmentˇ in effect at that point. The value stored in the location to which a
  1451. variable is bound is called the variable's value. By abuse of terminology, the variable is
  1452. sometimes said to name the value or to be bound to the value. This is not quite accurate, but
  1453. confusion rarely results from this practice.
  1454.  
  1455. Certain expression types are used to create new locations and to bind variables to those
  1456. locations. The most fundamental of these ˘binding constructsˇ is the lambda expression,
  1457. because all other binding constructs can be explained in terms of lambda expressions. The
  1458. other binding constructs are Íletˇ, Ílet*ˇ, Íletrecˇ, and Ídoˇ expressions (see sections 4.1.4,
  1459. 4.2.2, and 4.2.4).
  1460.  
  1461. Like Algol and Pascal, and unlike most other dialects of Lisp except for Common Lisp, Scheme
  1462. is a statically scoped language with block structure. To each place where a variable is bound
  1463. in a program there corresponds a ˘regionˇ of the program text within which the binding is
  1464. effective. The region is determined by the particular binding construct that establishes the
  1465. binding; if the binding is established by a lambda expression, for example, then its region is
  1466. the entire lambda expression. Every reference to or assignment of a variable refers to the
  1467. binding of the variable that established the innermost of the regions containing the use. If
  1468. there is no binding of the variable whose region contains the use, then the use refers to the
  1469. binding for the variable in the top level environment, if any (section 6); if there is no binding
  1470. for the identifier, it is said to be ˘unboundˇ.
  1471.  
  1472.  
  1473. Û3.2. True and falseˇ
  1474.  
  1475. Any Scheme value can be used as a boolean value for the purpose of a conditional test. As
  1476. explained in section 6.1, all values count as true in such a test except for Í#fˇ. This report
  1477. uses the word ``true'' to refer to any Scheme value that counts as true, and the word
  1478. ``false'' to refer to Í#fˇ.
  1479.  
  1480. ˘Note:ˇ In some implementations the empty list also counts as false instead of true.
  1481.  
  1482.  
  1483. Û3.3. External representationsˇ
  1484.  
  1485. An important concept in Scheme (and Lisp) is that of the ˘external representationˇ of an object
  1486. as a sequence of characters. For example, an external representation of the integer 28 is the
  1487. sequence of characters ``Í28ˇ'', and an external representation of a list consisting of the
  1488. integers 8 and 13 is the sequence of characters ``Í(8 13)ˇ''.
  1489.  
  1490. The external representation of an object is not necessarily unique. The integer 28 also has
  1491. representations ``Í#e28.000ˇ'' and ``Í#x1cˇ'', and the list in the previous paragraph also has
  1492. the representations ``Í( 08 13 )ˇ'' and ``Í(8 . (13 . ()))ˇ'' (see section 6.3).
  1493.  
  1494. Many objects have standard external representations, but some, such as procedures, do not
  1495. have standard representations (although particular implementations may define
  1496. representations for them).
  1497.  
  1498. An external representation may be written in a program to obtain the corresponding object
  1499. (see Íquoteˇ, section 4.1.2).
  1500.  
  1501. External representations can also be used for input and output. The procedure Íreadˇ (section
  1502. 6.10.2) parses external representations, and the procedure Íwriteˇ (section 6.10.3)
  1503. generates them. Together, they provide an elegant and powerful input/output facility.
  1504.  
  1505. Note that the sequence of characters ``Í(+ 2 6)ˇ'' is ˘notˇ an external representation of the
  1506. integer 8, even though it ˘isˇ an expression evaluating to the integer 8; rather, it is an
  1507. external representation of a three-element list, the elements of which are the symbol Í+ˇ and
  1508. the integers 2 and 6. Scheme's syntax has the property that any sequence of characters that
  1509. is an expression is also the external representation of some object. This can lead to
  1510. confusion, since it may not be obvious out of context whether a given sequence of characters
  1511. is intended to denote data or program, but it is also a source of power, since it facilitates
  1512. writing programs such as interpreters and compilers that treat programs as data (or vice
  1513. versa).
  1514.  
  1515. The syntax of external representations of various kinds of objects accompanies the
  1516. description of the primitives for manipulating the objects in the appropriate sections of
  1517. chapter 6.
  1518.  
  1519.  
  1520. Û3.4. Disjointness of typesˇ
  1521.  
  1522. No object satisfies more than one of the following predicates:
  1523.  
  1524. Í    boolean?          pair?
  1525. Í    symbol?           number?
  1526. Í    char?             string?
  1527. Í    vector?           procedure?ˇ
  1528.  
  1529. These predicates define the types ˘booleanˇ, ˘pairˇ, ˘symbolˇ, ˘numberˇ, ˘charˇ (or ˘characterˇ),
  1530. ˘stringˇ, ˘vectorˇ, and ˘procedureˇ.
  1531.  
  1532.  
  1533. Û3.5. Storage modelˇ
  1534.  
  1535. Variables and objects such as pairs, vectors, and strings implicitly denote locations or
  1536. sequences of locations. A string, for example, denotes as many locations as there are
  1537. characters in the string. (These locations need not correspond to a full machine word.) A new
  1538. value may be stored into one of these locations using the Ístring-set!ˇ procedure, but the
  1539. string continues to denote the same locations as before.
  1540.  
  1541. An object fetched from a location, by a variable reference or by a procedure such as Ícarˇ,
  1542. Ívector-refˇ, or Ístring-refˇ, is equivalent in the sense of Íeqv?ˇ (section 6.2) to the object
  1543. last stored in the location before the fetch.
  1544.  
  1545. Every location is marked to show whether it is in use. No variable or object ever refers to a
  1546. location that is not in use. Whenever this report speaks of storage being allocated for a
  1547. variable or object, what is meant is that an appropriate number of locations are chosen from
  1548. the set of locations that are not in use, and the chosen locations are marked to indicate that
  1549. they are now in use before the variable or object is made to denote them.
  1550.  
  1551. In many systems it is desirable for constants (i.e. the values of literal expressions) to
  1552. reside in read-only-memory. To express this, it is convenient to imagine that every object
  1553. that denotes locations is associated with a flag telling whether that object is mutable or
  1554. immutable. The constants and the strings returned by Ísymbol->stringˇ are then the
  1555. immutable objects, while all objects created by the other procedures listed in this report are
  1556. mutable. It is an error to attempt to store a new value into a location that is denoted by an
  1557. immutable object.
  1558.  
  1559.  
  1560. Û4. Expressionsˇ
  1561.  
  1562. A Scheme expression is a construct that returns a value, such as a variable reference,
  1563. literal, procedure call, or conditional.
  1564.  
  1565. Expression types are categorized as ˘primitiveˇ or ˘derivedˇ. Primitive expression types
  1566. include variables and procedure calls. Derived expression types are not semantically
  1567. primitive, but can instead be explained in terms of the primitive constructs as in section 7.3.
  1568. They are redundant in the strict sense of the word, but they capture common patterns of
  1569. usage, and are therefore provided as convenient abbreviations.
  1570.  
  1571.  
  1572. Û4.1. Primitive expression typesˇ
  1573.  
  1574.  
  1575. ¸4.1.1. Variable referencesˇ
  1576.  
  1577.  
  1578. <variable>Í                                                ˇessential syntax
  1579.  
  1580. An expression consisting of a variable (section 3.1) is a variable reference. The value of the
  1581. variable reference is the value stored in the location to which the variable is bound. It is an
  1582. error to reference an unbound variable.
  1583.  
  1584. Í    (define x 28)
  1585. Í    x                           ==>  28ˇ
  1586.  
  1587.  
  1588. ¸4.1.2. Literal expressionsˇ
  1589.  
  1590.  
  1591. Í(quote˘ ˇ<datum>Í)                                           ˇessential syntax
  1592. Í'ˇ<datum>Í                                                 ˇessential syntax
  1593. <constant>Í                                                ˇessential syntax
  1594.  
  1595. Í(quote ˇ<datum>Í)ˇ evaluates to <datum>. <Datum> may be any external representation of a
  1596. Scheme object (see section 3.3). This notation is used to include literal constants in Scheme
  1597. code.
  1598.  
  1599. Í    (quote a)                   ==>  a
  1600. Í    (quote #(a b c))            ==>  #(a b c)
  1601. Í    (quote (+ 1 2))             ==>  (+ 1 2)ˇ
  1602.  
  1603. Í(quote ˇ<datum>Í)ˇ may be abbreviated as Í'ˇ<datum>. The two notations are equivalent in all
  1604. respects.
  1605.  
  1606. Í    'a                          ==>  a
  1607. Í    '#(a b c)                   ==>  #(a b c)
  1608. Í    '()                         ==>  ()
  1609. Í    '(+ 1 2)                    ==>  (+ 1 2)
  1610. Í    '(quote a)                  ==>  (quote a)
  1611. Í    ''a                         ==>  (quote a)ˇ
  1612.  
  1613. Numerical constants, string constants, character constants, and boolean constants evaluate
  1614. ``to themselves''; they need not be quoted.
  1615.  
  1616. Í    '"abc"                      ==>  "abc"
  1617. Í    "abc"                       ==>  "abc"
  1618. Í    '145932                     ==>  145932
  1619. Í    145932                      ==>  145932
  1620. Í    '#t                         ==>  #t
  1621. Í    #t                          ==>  #tˇ
  1622.  
  1623. As noted in section 3.5, it is an error to alter a constant (i.e. the value of a literal
  1624. expression) using a mutation procedure like Íset-car!ˇ or Ístring-set!ˇ.
  1625.  
  1626.  
  1627. ¸4.1.3. Procedure callsˇ
  1628.  
  1629.  
  1630. Í(ˇ<operator>Í ˇ<operand˛1ˇ>Í ...)                                 ˇessential syntax
  1631.  
  1632. A procedure call is written by simply enclosing in parentheses expressions for the procedure
  1633. to be called and the arguments to be passed to it. The operator and operand expressions are
  1634. evaluated (in an unspecified order) and the resulting procedure is passed the resulting
  1635. arguments.
  1636.  
  1637. Í    (+ 3 4)                     ==>  7
  1638. Í    ((if #f + *) 3 4)           ==>  12ˇ
  1639.  
  1640. A number of procedures are available as the values of variables in the initial environment;
  1641. for example, the addition and multiplication procedures in the above examples are the values
  1642. of the variables Í+ˇ and Í*ˇ. New procedures are created by evaluating lambda expressions (see
  1643. section 4.1.4).
  1644.  
  1645. Procedure calls are also called ˘combinationsˇ.
  1646.  
  1647. ˘Note:ˇ In contrast to other dialects of Lisp, the order of evaluation is unspecified, and the
  1648. operator expression and the operand expressions are always evaluated with the same
  1649. evaluation rules.
  1650.  
  1651. ˘Note:ˇ Although the order of evaluation is otherwise unspecified, the effect of any concurrent
  1652. evaluation of the operator and operand expressions is constrained to be consistent with some
  1653. sequential order of evaluation. The order of evaluation may be chosen differently for each
  1654. procedure call.
  1655.  
  1656. ˘Note:ˇ In many dialects of Lisp, the empty combination, Í()ˇ, is a legitimate expression. In
  1657. Scheme, combinations must have at least one subexpression, so Í()ˇ is not a syntactically
  1658. valid expression.
  1659.  
  1660.  
  1661. ¸4.1.4. Lambda expressionsˇ
  1662.  
  1663.  
  1664. Í(lambda˘ ˇ<formals>˘ ˇ<body>Í)                                   ˇessential syntax
  1665.  
  1666. ˘Syntax:ˇ <Formals> should be a formal arguments list as described below, and <body> should
  1667. be a sequence of one or more expressions.
  1668.  
  1669. ˘Semantics:ˇ A lambda expression evaluates to a procedure. The environment in effect when
  1670. the lambda expression was evaluated is remembered as part of the procedure. When the
  1671. procedure is later called with some actual arguments, the environment in which the lambda
  1672. expression was evaluated will be extended by binding the variables in the formal argument
  1673. list to fresh locations, the corresponding actual argument values will be stored in those
  1674. locations, and the expressions in the body of the lambda expression will be evaluated
  1675. sequentially in the extended environment. The result of the last expression in the body will
  1676. be returned as the result of the procedure call.
  1677.  
  1678. Í    (lambda (x) (+ x x))        ==>  ˘a procedureÍ
  1679. Í    ((lambda (x) (+ x x)) 4)    ==>  8
  1680. Í    
  1681. Í    (define reverse-subtract
  1682. Í      (lambda (x y) (- y x)))
  1683. Í    (reverse-subtract 7 10)     ==>  3
  1684. Í    
  1685. Í    (define add4
  1686. Í      (let ((x 4))
  1687. Í        (lambda (y) (+ x y))))
  1688. Í    (add4 6)                    ==>  10ˇ
  1689.  
  1690. <Formals> should have one of the following forms:
  1691.  
  1692. • Í(ˇ<variable˛1ˇ>Í ...)ˇ: The procedure takes a fixed number of arguments; when the
  1693. procedure is called, the arguments will be stored in the bindings of the corresponding
  1694. variables.
  1695.  
  1696. • <variable>: The procedure takes any number of arguments; when the procedure is called,
  1697. the sequence of actual arguments is converted into a newly allocated list, and the list is
  1698. stored in the binding of the <variable>.
  1699.  
  1700. • Í(ˇ<variable˛1ˇ>Í ... ˇ<variable˛n-1ˇ>Í . ˇ<variable˛nˇ>Í)ˇ: If a space-delimited period precedes
  1701. the last variable, then the value stored in the binding of the last variable will be a newly
  1702. allocated list of the actual arguments left over after all the other actual arguments have
  1703. been matched up against the other formal arguments.
  1704.  
  1705. It is an error for a <variable> to appear more than once in <formals>.
  1706.  
  1707. Í    ((lambda x x) 3 4 5 6)      ==>  (3 4 5 6)
  1708. Í    ((lambda (x y . z) z)
  1709. Í     3 4 5 6)                   ==>  (5 6)ˇ
  1710.  
  1711. Each procedure created as the result of evaluating a lambda expression is tagged with a
  1712. storage location, in order to make Íeqv?ˇ and Íeq?ˇ work on procedures (see section 6.2).
  1713.  
  1714.  
  1715. ¸4.1.5. Conditionalsˇ
  1716.  
  1717.  
  1718. Í(if˘ ˇ<test>˘ ˇ<consequent>˘ ˇ<alternate>Í)                            ˇessential syntax
  1719. Í(if˘ ˇ<test>˘ ˇ<consequent>Í)                                            ˇsyntax
  1720.  
  1721. ˘Syntax:ˇ <Test>, <consequent>, and <alternate> may be arbitrary expressions.
  1722.  
  1723. ˘Semantics:ˇ An Íifˇ expression is evaluated as follows: first, <test> is evaluated. If it yields a
  1724. true value (see section 6.1), then <consequent> is evaluated and its value is returned.
  1725. Otherwise <alternate> is evaluated and its value is returned. If <test> yields a false value
  1726. and no <alternate> is specified, then the result of the expression is unspecified.
  1727.  
  1728. Í    (if (> 3 2) 'yes 'no)       ==>  yes
  1729. Í    (if (> 2 3) 'yes 'no)       ==>  no
  1730. Í    (if (> 3 2)
  1731. Í        (- 3 2)
  1732. Í        (+ 3 2))                ==>  1ˇ
  1733.  
  1734.  
  1735. ¸4.1.6. Assignmentsˇ
  1736.  
  1737.  
  1738. Í(set!˘ ˇ<variable>˘ ˇ<expression>Í)                                ˇessential syntax
  1739.  
  1740. <Expression> is evaluated, and the resulting value is stored in the location to which
  1741. <variable> is bound. <Variable> must be bound either in some region enclosing the Íset!ˇ
  1742. expression or at top level. The result of the Íset!ˇ expression is unspecified.
  1743.  
  1744. Í    (define x 2)
  1745. Í    (+ x 1)                     ==>  3
  1746. Í    (set! x 4)                  ==>  ˘unspecifiedÍ
  1747. Í    (+ x 1)                     ==>  5ˇ
  1748.  
  1749.  
  1750. Û4.2. Derived expression typesˇ
  1751.  
  1752. For reference purposes, section 7.3 gives rewrite rules that will convert constructs
  1753. described in this section into the primitive constructs described in the previous section.
  1754.  
  1755.  
  1756. ¸4.2.1. Conditionalsˇ
  1757.  
  1758.  
  1759. Í(cond˘ ˇ<clause˛1ˇ>˘ ˇ<clause˛2ˇ>˘ ...Í)                                 ˇessential syntax
  1760.  
  1761. ˘Syntax:ˇ Each <clause> should be of the form Í(ˇ<test>Í ˇ<expression>Í ...)ˇ where <test> is
  1762. any expression. The last <clause> may be an ``else clause,'' which has the form Í(else
  1763. ͡<expression˛1ˇ>Í ˇ<expression˛2ˇ>Í ...).ˇ
  1764.  
  1765. ˘Semantics:ˇ A Ícondˇ expression is evaluated by evaluating the <test> expressions of
  1766. successive <clause>s in order until one of them evaluates to a true value (see section 6.1).
  1767. When a <test> evaluates to a true value, then the remaining <expression>s in its <clause> are
  1768. evaluated in order, and the result of the last <expression> in the <clause> is returned as the
  1769. result of the entire Ícondˇ expression. If the selected <clause> contains only the <test> and no
  1770. <expression>s, then the value of the <test> is returned as the result. If all <test>s evaluate
  1771. to false values, and there is no else clause, then the result of the conditional expression is
  1772. unspecified; if there is an else clause, then its <expression>s are evaluated, and the value of
  1773. the last one is returned.
  1774.  
  1775. Í    (cond ((> 3 2) 'greater)
  1776. Í          ((< 3 2) 'less))      ==>  greater
  1777. Í    (cond ((> 3 3) 'greater)
  1778. Í          ((< 3 3) 'less)
  1779. Í          (else 'equal))        ==>  equalˇ
  1780.  
  1781. Some implementations support an alternative <clause> syntax, Í(ˇ<test>Í => ˇ<recipient>Í)ˇ,
  1782. where <recipient> is an expression. If <test> evaluates to a true value, then <recipient> is
  1783. evaluated. Its value must be a procedure of one argument; this procedure is then invoked on
  1784. the value of the <test>.
  1785.  
  1786. Í    (cond ((assv 'b '((a 1) (b 2))) => cadr)
  1787. Í          (else #f))            ==>  2ˇ
  1788.  
  1789.  
  1790. Í(case˘ ˇ<key>˘ ˇ<clause˛1ˇ>˘ ˇ<clause˛2ˇ>˘ ...Í)                            ˇessential syntax
  1791.  
  1792. ˘Syntax:ˇ <Key> may be any expression. Each <clause> should have the form Í((ˇ<datum˛1ˇ>Í
  1793. Í...) ˇ<expression˛1ˇ>Í ˇ<expression˛2ˇ>Í ...),ˇ where each <datum> is an external
  1794. representation of some object. All the <datum>s must be distinct. The last <clause> may be
  1795. an ``else clause,'' which has the form Í(else ˇ<expression˛1ˇ>Í ˇ<expression˛2ˇ>Í ...).ˇ
  1796.  
  1797. ˘Semantics:ˇ A Ícaseˇ expression is evaluated as follows. <Key> is evaluated and its result is
  1798. compared against each <datum>. If the result of evaluating <key> is equivalent (in the sense
  1799. of Íeqv?ˇ; see section 6.2) to a <datum>, then the expressions in the corresponding <clause>
  1800. are evaluated from left to right and the result of the last expression in the <clause> is
  1801. returned as the result of the Ícaseˇ expression. If the result of evaluating <key> is different
  1802. from every <datum>, then if there is an else clause its expressions are evaluated and the
  1803. result of the last is the result of the Ícaseˇ expression; otherwise the result of the Ícaseˇ
  1804. expression is unspecified.
  1805.  
  1806. Í    (case (* 2 3)
  1807. Í      ((2 3 5 7) 'prime)
  1808. Í      ((1 4 6 8 9) 'composite)) ==>  composite
  1809. Í    (case (car '(c d))
  1810. Í      ((a) 'a)
  1811. Í      ((b) 'b))                 ==>  ˘unspecifiedÍ
  1812. Í    (case (car '(c d))
  1813. Í      ((a e i o u) 'vowel)
  1814. Í      ((w y) 'semivowel)
  1815. Í      (else 'consonant))        ==>  consonantˇ
  1816.  
  1817.  
  1818. Í(and˘ ˇ<test˛1ˇ>˘ ...Í)                                           ˇessential syntax
  1819.  
  1820. The <test> expressions are evaluated from left to right, and the value of the first expression
  1821. that evaluates to a false value (see section 6.1) is returned. Any remaining expressions are
  1822. not evaluated. If all the expressions evaluate to true values, the value of the last expression
  1823. is returned. If there are no expressions then Í#tˇ is returned.
  1824.  
  1825. Í    (and (= 2 2) (> 2 1))       ==>  #t
  1826. Í    (and (= 2 2) (< 2 1))       ==>  #f
  1827. Í    (and 1 2 'c '(f g))         ==>  (f g)
  1828. Í    (and)                       ==>  #tˇ
  1829.  
  1830.  
  1831. Í(or˘ ˇ<test˛1ˇ>˘ ...Í)                                            ˇessential syntax
  1832.  
  1833. The <test> expressions are evaluated from left to right, and the value of the first expression
  1834. that evaluates to a true value (see section 6.1) is returned. Any remaining expressions are
  1835. not evaluated. If all expressions evaluate to false values, the value of the last expression is
  1836. returned. If there are no expressions then Í#fˇ is returned.
  1837.  
  1838. Í    (or (= 2 2) (> 2 1))        ==>  #t
  1839. Í    (or (= 2 2) (< 2 1))        ==>  #t
  1840. Í    (or #f #f #f)               ==>  #f
  1841. Í    (or (memq 'b '(a b c)) 
  1842. Í        (/ 3 0))                ==>  (b c)ˇ
  1843.  
  1844.  
  1845. ¸4.2.2. Binding constructsˇ
  1846.  
  1847. The three binding constructs Íletˇ, Ílet*ˇ, and Íletrecˇ give Scheme a block structure, like
  1848. Algol 60. The syntax of the three constructs is identical, but they differ in the regions they
  1849. establish for their variable bindings. In a Íletˇ expression, the initial values are computed
  1850. before any of the variables become bound; in a Ílet*ˇ expression, the bindings and evaluations
  1851. are performed sequentially; while in a Íletrecˇ expression, all the bindings are in effect
  1852. while their initial values are being computed, thus allowing mutually recursive definitions.
  1853.  
  1854.  
  1855. Í(let˘ ˇ<bindings>˘ ˇ<body>Í)                                     ˇessential syntax
  1856.  
  1857. ˘Syntax:ˇ <Bindings> should have the form Í((ˇ<variable˛1ˇ>Í ˇ<init˛1ˇ>Í) ...),ˇ where each <init>
  1858. is an expression, and <body> should be a sequence of one or more expressions. It is an error
  1859. for a <variable> to appear more than once in the list of variables being bound.
  1860.  
  1861. ˘Semantics:ˇ The <init>s are evaluated in the current environment (in some unspecified order),
  1862. the <variable>s are bound to fresh locations holding the results, the <body> is evaluated in
  1863. the extended environment, and the value of the last expression of <body> is returned. Each
  1864. binding of a <variable> has <body> as its region.
  1865.  
  1866. Í    (let ((x 2) (y 3))
  1867. Í      (* x y))                  ==>  6
  1868. Í    
  1869. Í    (let ((x 2) (y 3))
  1870. Í      (let ((x 7)
  1871. Í            (z (+ x y)))
  1872. Í        (* z x)))               ==>  35ˇ
  1873.  
  1874. See also named Íletˇ, section 4.2.4.
  1875.  
  1876.  
  1877. Í(let*˘ ˇ<bindings>˘ ˇ<body>Í)                                            ˇsyntax
  1878.  
  1879. ˘Syntax:ˇ <Bindings> should have the form Í((ˇ<variable˛1ˇ>Í ˇ<init˛1ˇ>Í) ...),ˇ and <body> should
  1880. be a sequence of one or more expressions.
  1881.  
  1882. ˘Semantics:ˇ ÍLet*ˇ is similar to Íletˇ, but the bindings are performed sequentially from left to
  1883. right, and the region of a binding indicated by Í(ˇ<variable>Í ˇ<init>Í)ˇ is that part of the Ílet*ˇ
  1884. expression to the right of the binding. Thus the second binding is done in an environment in
  1885. which the first binding is visible, and so on.
  1886.  
  1887. Í    (let ((x 2) (y 3))
  1888. Í      (let* ((x 7)
  1889. Í             (z (+ x y)))
  1890. Í        (* z x)))               ==>  70ˇ
  1891.  
  1892.  
  1893. Í(letrec˘ ˇ<bindings>˘ ˇ<body>Í)                                  ˇessential syntax
  1894.  
  1895. ˘Syntax:ˇ <Bindings> should have the form Í((ˇ<variable˛1ˇ>Í ˇ<init˛1ˇ>Í) ...),ˇ and <body> should
  1896. be a sequence of one or more expressions. It is an error for a <variable> to appear more than
  1897. once in the list of variables being bound.
  1898.  
  1899. ˘Semantics:ˇ The <variable>s are bound to fresh locations holding undefined values, the <init>s
  1900. are evaluated in the resulting environment (in some unspecified order), each <variable> is
  1901. assigned to the result of the corresponding <init>, the <body> is evaluated in the resulting
  1902. environment, and the value of the last expression in <body> is returned. Each binding of a
  1903. <variable> has the entire Íletrecˇ expression as its region, making it possible to define
  1904. mutually recursive procedures.
  1905.  
  1906. Í    (letrec ((even?
  1907. Í              (lambda (n)
  1908. Í                (if (zero? n)
  1909. Í                    #t
  1910. Í                    (odd? (- n 1)))))
  1911. Í             (odd?
  1912. Í              (lambda (n)
  1913. Í                (if (zero? n)
  1914. Í                    #f
  1915. Í                    (even? (- n 1))))))
  1916. Í      (even? 88))   
  1917. Í                                ==>  #tˇ
  1918.  
  1919. One restriction on Íletrecˇ is very important: it must be possible to evaluate each <init>
  1920. without assigning or referring to the value of any <variable>. If this restriction is violated,
  1921. then it is an error. The restriction is necessary because Scheme passes arguments by value
  1922. rather than by name. In the most common uses of Íletrecˇ, all the <init>s are lambda
  1923. expressions and the restriction is satisfied automatically.
  1924.  
  1925.  
  1926. ¸4.2.3. Sequencingˇ
  1927.  
  1928.  
  1929. Í(begin˘ ˇ<expression˛1ˇ>˘ ˇ<expression˛2ˇ>˘ ...Í)                         ˇessential syntax
  1930.  
  1931. The <expression>s are evaluated sequentially from left to right, and the value of the last
  1932. <expression> is returned. This expression type is used to sequence side effects such as input
  1933. and output.
  1934.  
  1935. Í    (define x 0)
  1936. Í    
  1937. Í    (begin (set! x 5)
  1938. Í           (+ x 1))             ==>  6
  1939. Í    
  1940. Í    (begin (display "4 plus 1 equals ")
  1941. Í           (display (+ 4 1)))   ==>  ˘unspecifiedÍ
  1942. Í               ˘and printsÍ  4 plus 1 equals 5ˇ
  1943.  
  1944. ˘Note:ˇ [SICP] uses the keyword Ísequenceˇ instead of Íbeginˇ.
  1945.  
  1946.  
  1947. ¸4.2.4. Iterationˇ
  1948.  
  1949. Í(do ((ˇ<variable˛1ˇ>Í ˇ<init˛1ˇ>Í ˇ<step˛1ˇ>Í)                                   ˇsyntax Í
  1950. Í     ...)
  1951. Í    (ˇ<test>Í ˇ<expression>Í ...)
  1952. Í  ˇ<command>Í ...)ˇ
  1953.  
  1954. ÍDoˇ is an iteration construct. It specifies a set of variables to be bound, how they are to be
  1955. initialized at the start, and how they are to be updated on each iteration. When a termination
  1956. condition is met, the loop exits with a specified result value.
  1957.  
  1958. ÍDoˇ expressions are evaluated as follows: The <init> expressions are evaluated (in some
  1959. unspecified order), the <variable>s are bound to fresh locations, the results of the <init>
  1960. expressions are stored in the bindings of the <variable>s, and then the iteration phase begins.
  1961.  
  1962. Each iteration begins by evaluating <test>; if the result is false (see section 6.1), then the
  1963. <command> expressions are evaluated in order for effect, the <step> expressions are
  1964. evaluated in some unspecified order, the <variable>s are bound to fresh locations, the
  1965. results of the <step>s are stored in the bindings of the <variable>s, and the next iteration
  1966. begins.
  1967.  
  1968. If <test> evaluates to a true value, then the <expression>s are evaluated from left to right
  1969. and the value of the last <expression> is returned as the value of the Ídoˇ expression. If no
  1970. <expression>s are present, then the value of the Ídoˇ expression is unspecified.
  1971.  
  1972. The region of the binding of a <variable> consists of the entire Ídoˇ expression except for the
  1973. <init>s. It is an error for a <variable> to appear more than once in the list of Ídoˇ variables.
  1974.  
  1975. A <step> may be omitted, in which case the effect is the same as if Í(ˇ<variable>Í ˇ<init>Í
  1976. ͡<variable>Í)ˇ had been written instead of Í(ˇ<variable>Í ˇ<init>Í)ˇ.
  1977.  
  1978. Í    (do ((vec (make-vector 5))
  1979. Í         (i 0 (+ i 1)))
  1980. Í        ((= i 5) vec)
  1981. Í      (vector-set! vec i i))    ==>  #(0 1 2 3 4)
  1982. Í    
  1983. Í    (let ((x '(1 3 5 7 9)))
  1984. Í      (do ((x x (cdr x))
  1985. Í           (sum 0 (+ sum (car x))))
  1986. Í          ((null? x) sum)))     ==>  25ˇ
  1987.  
  1988.  
  1989. Í(let˘ ˇ<variable>˘ ˇ<bindings>˘ ˇ<body>Í)                                    ˇsyntax
  1990.  
  1991. Some implementations of Scheme permit a variant on the syntax of Íletˇ called ``named Íletˇ''
  1992. which provides a more general looping construct than Ídoˇ, and may also be used to express
  1993. recursions.
  1994.  
  1995. Named Íletˇ has the same syntax and semantics as ordinary Íletˇ except that <variable> is
  1996. bound within <body> to a procedure whose formal arguments are the bound variables and
  1997. whose body is <body>. Thus the execution of <body> may be repeated by invoking the
  1998. procedure named by <variable>.
  1999.  
  2000. Í    (let loop ((numbers '(3 -2 1 6 -5))
  2001. Í               (nonneg '())
  2002. Í               (neg '()))
  2003. Í      (cond ((null? numbers) (list nonneg neg))
  2004. Í            ((>= (car numbers) 0)
  2005. Í             (loop (cdr numbers)
  2006. Í                   (cons (car numbers) nonneg)
  2007. Í                   neg))
  2008. Í            ((< (car numbers) 0)
  2009. Í             (loop (cdr numbers)
  2010. Í                   nonneg
  2011. Í                   (cons (car numbers) neg)))))   
  2012. Í              ==>  ((6 1 3) (-5 -2))ˇ
  2013.  
  2014.  
  2015. ¸4.2.5. Delayed evaluationˇ
  2016.  
  2017.  
  2018. Í(delay˘ ˇ<expression>Í)                                              ˇsyntax
  2019.  
  2020. The Ídelayˇ construct is used together with the procedure Íforceˇ to implement ˘lazy
  2021. ˘evaluationˇ or ˘call by needˇ. Í(delay ˇ<expression>Í)ˇ returns an object called a ˘promiseˇ which
  2022. at some point in the future may be asked (by the Íforceˇ procedure) to evaluate <expression>
  2023. and deliver the resulting value.
  2024.  
  2025. See the description of Íforceˇ (section 6.9) for a more complete description of Ídelayˇ.
  2026.  
  2027.  
  2028. ¸4.2.6. Quasiquotationˇ
  2029.  
  2030.  
  2031. Í(quasiquote˘ ˇ<template>Í)                                    ˇessential syntax 
  2032. Í`ˇ<template>Í                                               ˇessential syntax
  2033.  
  2034. ``Backquote'' or ``quasiquote'' expressions are useful for constructing a list or vector
  2035. structure when most but not all of the desired structure is known in advance. If no commas
  2036. appear within the <template>, the result of evaluating Í`ˇ<template> is equivalent to the result
  2037. of evaluating Í'ˇ<template>. If a comma appears within the <template>, however, the
  2038. expression following the comma is evaluated (``unquoted'') and its result is inserted into the
  2039. structure instead of the comma and the expression. If a comma appears followed immediately
  2040. by an at-sign (Í@ˇ), then the following expression must evaluate to a list; the opening and
  2041. closing parentheses of the list are then ``stripped away'' and the elements of the list are
  2042. inserted in place of the comma at-sign expression sequence.
  2043.  
  2044. Í    `(list ,(+ 1 2) 4)          ==>  (list 3 4)
  2045. Í    (let ((name 'a)) `(list ,name ',name))           
  2046. Í              ==>  (list a (quote a))
  2047. Í    `(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b)           
  2048. Í              ==>  (a 3 4 5 6 b)
  2049. Í    `((foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons)))           
  2050. Í              ==>  ((foo 7) . cons)
  2051. Í    `#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8)           
  2052. Í              ==>  #(10 5 2 4 3 8)ˇ
  2053.  
  2054. Quasiquote forms may be nested. Substitutions are made only for unquoted components
  2055. appearing at the same nesting level as the outermost backquote. The nesting level increases
  2056. by one inside each successive quasiquotation, and decreases by one inside each unquotation.
  2057.  
  2058. Í    `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)           
  2059. Í              ==>  (a `(b ,(+ 1 2) ,(foo 4 d) e) f)
  2060. Í    (let ((name1 'x)
  2061. Í          (name2 'y))
  2062. Í      `(a `(b ,,name1 ,',name2 d) e))           
  2063. Í              ==>  (a `(b ,x ,'y d) e)ˇ
  2064.  
  2065. The notations Í`ˇ<template> and Í(quasiquote ˇ<template>Í)ˇ are identical in all respects.
  2066. Í,ˇ<expression> is identical to Í(unquote ˇ<expression>Í)ˇ, and Í,@ˇ<expression> is identical to
  2067. Í(unquote-splicing ˇ<expression>Í)ˇ. The external syntax generated by Íwriteˇ for
  2068. two-element lists whose car is one of these symbols may vary between implementations.
  2069.  
  2070. Í    (quasiquote (list (unquote (+ 1 2)) 4))           
  2071. Í              ==>  (list 3 4)
  2072. Í    '(quasiquote (list (unquote (+ 1 2)) 4))           
  2073. Í              ==>  `(list ,(+ 1 2) 4)
  2074. Í         ˘i.e.,Í (quasiquote (list (unquote (+ 1 2)) 4))ˇ
  2075.  
  2076. Unpredictable behavior can result if any of the symbols Íquasiquoteˇ, Íunquoteˇ, or
  2077. Íunquote-splicingˇ appear in positions within a <template> otherwise than as described
  2078. above.
  2079.  
  2080.  
  2081. Û5. Program structureˇ
  2082.  
  2083.  
  2084. Û5.1. Programsˇ
  2085.  
  2086. A Scheme program consists of a sequence of expressions and definitions. Expressions are
  2087. described in chapter 4; definitions are the subject of the rest of the present chapter.
  2088.  
  2089. Programs are typically stored in files or entered interactively to a running Scheme system,
  2090. although other paradigms are possible; questions of user interface lie outside the scope of
  2091. this report. (Indeed, Scheme would still be useful as a notation for expressing computational
  2092. methods even in the absence of a mechanical implementation.)
  2093.  
  2094. Definitions occurring at the top level of a program can be interpreted declaratively. They
  2095. cause bindings to be created in the top level environment. Expressions occurring at the top
  2096. level of a program are interpreted imperatively; they are executed in order when the
  2097. program is invoked or loaded, and typically perform some kind of initialization.
  2098.  
  2099.  
  2100. Û5.2. Definitionsˇ
  2101.  
  2102. Definitions are valid in some, but not all, contexts where expressions are allowed. They are
  2103. valid only at the top level of a <program> and, in some implementations, at the beginning of a
  2104. <body>.
  2105.  
  2106. A definition should have one of the following forms:
  2107.  
  2108. • Í(define ˇ<variable>Í ˇ<expression>Í)ˇ
  2109.  
  2110. This syntax is essential.
  2111.  
  2112. • Í(define (ˇ<variable>Í ˇ<formals>Í) ˇ<body>Í)ˇ
  2113.  
  2114. This syntax is not essential. <Formals> should be either a sequence of zero or more
  2115. variables, or a sequence of one or more variables followed by a space-delimited period and
  2116. another variable (as in a lambda expression). This form is equivalent to
  2117.  
  2118. Í    (define ˇ<variable>Í
  2119. Í      (lambda (ˇ<formals>Í) ˇ<body>Í)).ˇ
  2120.  
  2121. • Í(define (ˇ<variable>Í . ˇ<formal>Í) ˇ<body>Í)ˇ
  2122.  
  2123. This syntax is not essential. <Formal> should be a single variable. This form is equivalent to
  2124.  
  2125. Í    (define ˇ<variable>Í
  2126. Í      (lambda ˇ<formal>Í ˇ<body>Í)).ˇ
  2127.  
  2128. • Í(begin ˇ<definition˛1ˇ>Í ...)ˇ
  2129.  
  2130. This syntax is essential. This form is equivalent to the set of definitions that form the body
  2131. of the Íbeginˇ.
  2132.  
  2133.  
  2134. ¸5.2.1. Top level definitionsˇ
  2135.  
  2136. At the top level of a program, a definition
  2137.  
  2138. Í    (define ˇ<variable>Í ˇ<expression>Í)ˇ
  2139.  
  2140. has essentially the same effect as the assignment expression
  2141.  
  2142. Í    (set! ˇ<variable>Í ˇ<expression>Í)ˇ
  2143.  
  2144. if <variable> is bound. If <variable> is not bound, however, then the definition will bind
  2145. <variable> to a new location before performing the assignment, whereas it would be an error
  2146. to perform a Íset!ˇ on an unbound variable.
  2147.  
  2148. Í    (define add3
  2149. Í      (lambda (x) (+ x 3)))
  2150. Í    (add3 3)                    ==>  6
  2151. Í    (define first car)
  2152. Í    (first '(1 2))              ==>  1ˇ
  2153.  
  2154. All Scheme implementations must support top level definitions.
  2155.  
  2156. Some implementations of Scheme use an initial environment in which all possible variables
  2157. are bound to locations, most of which contain undefined values. Top level definitions in such
  2158. an implementation are truly equivalent to assignments.
  2159.  
  2160.  
  2161. ¸5.2.2. Internal definitionsˇ
  2162.  
  2163. Some implementations of Scheme permit definitions to occur at the beginning of a <body>
  2164. (that is, the body of a Ílambdaˇ, Íletˇ, Ílet*ˇ, Íletrecˇ, or Ídefineˇ expression). Such
  2165. definitions are known as ˘internal definitionsˇ as opposed to the top level definitions described
  2166. above. The variable defined by an internal definition is local to the <body>. That is,
  2167. <variable> is bound rather than assigned, and the region of the binding is the entire <body>.
  2168. For example,
  2169.  
  2170. Í    (let ((x 5))
  2171. Í      (define foo (lambda (y) (bar x y)))
  2172. Í      (define bar (lambda (a b) (+ (* a b) a)))
  2173. Í      (foo (+ x 3)))            ==>  45ˇ
  2174.  
  2175. A <body> containing internal definitions can always be converted into a completely equivalent
  2176. Íletrecˇ expression. For example, the Íletˇ expression in the above example is equivalent to
  2177.  
  2178. Í    (let ((x 5))
  2179. Í      (letrec ((foo (lambda (y) (bar x y)))
  2180. Í               (bar (lambda (a b) (+ (* a b) a))))
  2181. Í        (foo (+ x 3))))ˇ
  2182.  
  2183. Just as for the equivalent Íletrecˇ expression, it must be possible to evaluate each
  2184. <expression> of every internal definition in a <body> without assigning or referring to the
  2185. value of any <variable> being defined.
  2186.  
  2187.  
  2188. Û6. Standard proceduresˇ
  2189.  
  2190. This chapter describes Scheme's built-in procedures. The initial (or ``top level'') Scheme
  2191. environment starts out with a number of variables bound to locations containing useful
  2192. values, most of which are primitive procedures that manipulate data. For example, the
  2193. variable Íabsˇ is bound to (a location initially containing) a procedure of one argument that
  2194. computes the absolute value of a number, and the variable Í+ˇ is bound to a procedure that
  2195. computes sums.
  2196.  
  2197.  
  2198. Û6.1. Booleansˇ
  2199.  
  2200. The standard boolean objects for true and false are written as Í#tˇ and Í#fˇ. What really
  2201. matters, though, are the objects that the Scheme conditional expressions (Íifˇ, Ícondˇ, Íandˇ,
  2202. Íorˇ, Ídoˇ) treat as true or false. The phrase ``a true value'' (or sometimes just ``true'') means
  2203. any object treated as true by the conditional expressions, and the phrase ``a false value'' (or
  2204. ``false'') means any object treated as false by the conditional expressions.
  2205.  
  2206. Of all the standard Scheme values, only Í#fˇ counts as false in conditional expressions. Except
  2207. for Í#fˇ, all standard Scheme values, including Í#tˇ, pairs, the empty list, symbols, numbers,
  2208. strings, vectors, and procedures, count as true.
  2209.  
  2210. ˘Note:ˇ In some implementations the empty list counts as false, contrary to the above.
  2211. Nonetheless a few examples in this report assume that the empty list counts as true, as in
  2212. [IEEE-Scheme].
  2213.  
  2214. ˘Note:ˇ Programmers accustomed to other dialects of Lisp should be aware that Scheme
  2215. distinguishes both Í#fˇ and the empty list from the symbol Ínilˇ.
  2216.  
  2217. Boolean constants evaluate to themselves, so they don't need to be quoted in programs.
  2218.  
  2219. Í    #t                          ==>  #t
  2220. Í    #f                          ==>  #f
  2221. Í    '#f                         ==>  #fˇ
  2222.  
  2223.  
  2224. Í(not˘ objÍ)                                              ˇessential procedure
  2225.  
  2226. ÍNotˇ returns Í#tˇ if ˘objˇ is false, and returns Í#fˇ otherwise.
  2227.  
  2228. Í    (not #t)                    ==>  #f
  2229. Í    (not 3)                     ==>  #f
  2230. Í    (not (list 3))              ==>  #f
  2231. Í    (not #f)                    ==>  #t
  2232. Í    (not '())                   ==>  #f
  2233. Í    (not (list))                ==>  #f
  2234. Í    (not 'nil)                  ==>  #fˇ
  2235.  
  2236.  
  2237. Í(boolean?˘ objÍ)                                         ˇessential procedure
  2238.  
  2239. ÍBoolean?ˇ returns Í#tˇ if ˘objˇ is either Í#tˇ or Í#fˇ and returns Í#fˇ otherwise.
  2240.  
  2241. Í    (boolean? #f)               ==>  #t
  2242. Í    (boolean? 0)                ==>  #f
  2243. Í    (boolean? '())              ==>  #fˇ
  2244.  
  2245.  
  2246. Û6.2. Equivalence predicatesˇ
  2247.  
  2248. A ˘predicateˇ is a procedure that always returns a boolean value (Í#tˇ or Í#fˇ). An ˘equivalence
  2249. ˘predicateˇ is the computational analogue of a mathematical equivalence relation (it is
  2250. symmetric, reflexive, and transitive). Of the equivalence predicates described in this
  2251. section, Íeq?ˇ is the finest or most discriminating, and Íequal?ˇ is the coarsest. ÍEqv?ˇ is
  2252. slightly less discriminating than Íeq?ˇ.
  2253.  
  2254.  
  2255. Í(eqv?˘ obj¯1˘ obj¯2Í)                                        ˇessential procedure
  2256.  
  2257. The Íeqv?ˇ procedure defines a useful equivalence relation on objects. Briefly, it returns Í#tˇ if
  2258. ˘obj¯1ˇ and ˘obj¯2ˇ should normally be regarded as the same object. This relation is left slightly
  2259. open to interpretation, but the following partial specification of Íeqv?ˇ holds for all
  2260. implementations of Scheme.
  2261.  
  2262. The Íeqv?ˇ procedure returns Í#tˇ if:
  2263.  
  2264. • ˘obj¯1ˇ and ˘obj¯2ˇ are both Í#tˇ or both Í#fˇ.
  2265.  
  2266. • ˘obj¯1ˇ and ˘obj¯2ˇ are both symbols and
  2267.  
  2268. Í    (string=? (symbol->string ˘obj¯1Í)
  2269. Í              (symbol->string ˘obj¯2Í))
  2270. Í                                ==>  #tˇ
  2271.  
  2272. ˘Note:ˇ This assumes that neither ˘obj¯1ˇ nor ˘obj¯2ˇ is an ``uninterned symbol'' as alluded to in
  2273. section 6.4. This report does not presume to specify the behavior of Íeqv?ˇ on
  2274. implementation-dependent extensions.
  2275.  
  2276. • ˘obj¯1ˇ and ˘obj¯2ˇ are both numbers, are numerically equal (see Í=ˇ, section 6.5), and are either
  2277. both exact or both inexact.
  2278.  
  2279. • ˘obj¯1ˇ and ˘obj¯2ˇ are both characters and are the same character according to the Íchar=?ˇ
  2280. procedure (section 6.6).
  2281.  
  2282. • both ˘obj¯1ˇ and ˘obj¯2ˇ are the empty list.
  2283.  
  2284. • ˘obj¯1ˇ and ˘obj¯2ˇ are pairs, vectors, or strings that denote the same locations in the store
  2285. (section 3.5).
  2286.  
  2287. • ˘obj¯1ˇ and ˘obj¯2ˇ are procedures whose location tags are equal (section 4.1.4).
  2288.  
  2289. The Íeqv?ˇ procedure returns Í#fˇ if:
  2290.  
  2291. • ˘obj¯1ˇ and ˘obj¯2ˇ are of different types (section 3.4).
  2292.  
  2293. • one of ˘obj¯1ˇ and ˘obj¯2ˇ is Í#tˇ but the other is Í#fˇ.
  2294.  
  2295. • ˘obj¯1ˇ and ˘obj¯2ˇ are symbols but
  2296.  
  2297. Í    (string=? (symbol->string ˘obj¯1Í)
  2298. Í              (symbol->string ˘obj¯2Í))
  2299. Í                                ==>  #fˇ
  2300.  
  2301. • one of ˘obj¯1ˇ and ˘obj¯2ˇ is an exact number but the other is an inexact number.
  2302.  
  2303. • ˘obj¯1ˇ and ˘obj¯2ˇ are numbers for which the Í=ˇ procedure returns Í#fˇ.
  2304.  
  2305. • ˘obj¯1ˇ and ˘obj¯2ˇ are characters for which the Íchar=?ˇ procedure returns Í#fˇ.
  2306.  
  2307. • one of ˘obj¯1ˇ and ˘obj¯2ˇ is the empty list but the other is not.
  2308.  
  2309. • ˘obj¯1ˇ and ˘obj¯2ˇ are pairs, vectors, or strings that denote distinct locations.
  2310.  
  2311. • ˘obj¯1ˇ and ˘obj¯2ˇ are procedures that would behave differently (return a different value or
  2312. have different side effects) for some arguments.
  2313.  
  2314. Í    (eqv? 'a 'a)                ==>  #t
  2315. Í    (eqv? 'a 'b)                ==>  #f
  2316. Í    (eqv? 2 2)                  ==>  #t
  2317. Í    (eqv? '() '())              ==>  #t
  2318. Í    (eqv? 100000000 100000000)  ==>  #t
  2319. Í    (eqv? (cons 1 2) (cons 1 2))==>  #f
  2320. Í    (eqv? (lambda () 1)
  2321. Í          (lambda () 2))        ==>  #f
  2322. Í    (eqv? #f 'nil)              ==>  #f
  2323. Í    (let ((p (lambda (x) x)))
  2324. Í      (eqv? p p))               ==>  #tˇ
  2325.  
  2326. The following examples illustrate cases in which the above rules do not fully specify the
  2327. behavior of Íeqv?ˇ. All that can be said about such cases is that the value returned by Íeqv?ˇ
  2328. must be a boolean.
  2329.  
  2330. Í    (eqv? "" "")                ==>  ˘unspecifiedÍ
  2331. Í    (eqv? '#() '#())            ==>  ˘unspecifiedÍ
  2332. Í    (eqv? (lambda (x) x)
  2333. Í          (lambda (x) x))       ==>  ˘unspecifiedÍ
  2334. Í    (eqv? (lambda (x) x)
  2335. Í          (lambda (y) y))       ==>  ˘unspecifiedˇ
  2336.  
  2337. The next set of examples shows the use of Íeqv?ˇ with procedures that have local state.
  2338. ÍGen-counterˇ must return a distinct procedure every time, since each procedure has its
  2339. own internal counter. ÍGen-loserˇ, however, returns equivalent procedures each time, since
  2340. the local state does not affect the value or side effects of the procedures.
  2341.  
  2342. Í    (define gen-counter
  2343. Í      (lambda ()
  2344. Í        (let ((n 0))
  2345. Í          (lambda () (set! n (+ n 1)) n))))
  2346. Í    (let ((g (gen-counter)))
  2347. Í      (eqv? g g))               ==>  #t
  2348. Í    (eqv? (gen-counter) (gen-counter))
  2349. Í                                ==>  #f
  2350. Í    (define gen-loser
  2351. Í      (lambda ()
  2352. Í        (let ((n 0))
  2353. Í          (lambda () (set! n (+ n 1)) 27))))
  2354. Í    (let ((g (gen-loser)))
  2355. Í      (eqv? g g))               ==>  #t
  2356. Í    (eqv? (gen-loser) (gen-loser))
  2357. Í                                ==>  ˘unspecifiedÍ
  2358. Í    
  2359. Í    (letrec ((f (lambda () (if (eqv? f g) 'both 'f)))
  2360. Í             (g (lambda () (if (eqv? f g) 'both 'g)))
  2361. Í      (eqv? f g))
  2362. Í                                ==>  ˘unspecifiedÍ
  2363. Í    
  2364. Í    (letrec ((f (lambda () (if (eqv? f g) 'f 'both)))
  2365. Í             (g (lambda () (if (eqv? f g) 'g 'both)))
  2366. Í      (eqv? f g))
  2367. Í                                ==>  #fˇ
  2368.  
  2369. Since it is an error to modify constant objects (those returned by literal expressions),
  2370. implementations are permitted, though not required, to share structure between constants
  2371. where appropriate. Thus the value of Íeqv?ˇ on constants is sometimes
  2372. implementation-dependent.
  2373.  
  2374. Í    (eqv? '(a) '(a))            ==>  ˘unspecifiedÍ
  2375. Í    (eqv? "a" "a")              ==>  ˘unspecifiedÍ
  2376. Í    (eqv? '(b) (cdr '(a b)))    ==>  ˘unspecifiedÍ
  2377. Í    (let ((x '(a)))
  2378. Í      (eqv? x x))               ==>  #tˇ
  2379.  
  2380. ˘Rationale:ˇ The above definition of Íeqv?ˇ allows implementations latitude in their treatment of
  2381. procedures and literals: implementations are free either to detect or to fail to detect that
  2382. two procedures or two literals are equivalent to each other, and can decide whether or not
  2383. to merge representations of equivalent objects by using the same pointer or bit pattern to
  2384. represent both.
  2385.  
  2386.  
  2387. Í(eq?˘ obj¯1˘ obj¯2Í)                                         ˇessential procedure
  2388.  
  2389. ÍEq?ˇ is similar to Íeqv?ˇ except that in some cases it is capable of discerning distinctions finer
  2390. than those detectable by Íeqv?ˇ.
  2391.  
  2392. ÍEq?ˇ and Íeqv?ˇ are guaranteed to have the same behavior on symbols, booleans, the empty
  2393. list, pairs, and non-empty strings and vectors. ÍEq?ˇ's behavior on numbers and characters is
  2394. implementation-dependent, but it will always return either true or false, and will return
  2395. true only when Íeqv?ˇ would also return true. ÍEq?ˇ may also behave differently from Íeqv?ˇ on
  2396. empty vectors and empty strings.
  2397.  
  2398. Í    (eq? 'a 'a)                 ==>  #t
  2399. Í    (eq? '(a) '(a))             ==>  ˘unspecifiedÍ
  2400. Í    (eq? (list 'a) (list 'a))   ==>  #f
  2401. Í    (eq? "a" "a")               ==>  ˘unspecifiedÍ
  2402. Í    (eq? "" "")                 ==>  ˘unspecifiedÍ
  2403. Í    (eq? '() '())               ==>  #t
  2404. Í    (eq? 2 2)                   ==>  ˘unspecifiedÍ
  2405. Í    (eq? #\A #\A)               ==>  ˘unspecifiedÍ
  2406. Í    (eq? car car)               ==>  #t
  2407. Í    (let ((n (+ 2 3)))
  2408. Í      (eq? n n))                ==>  ˘unspecifiedÍ
  2409. Í    (let ((x '(a)))
  2410. Í      (eq? x x))                ==>  #t
  2411. Í    (let ((x '#()))
  2412. Í      (eq? x x))                ==>  #t
  2413. Í    (let ((p (lambda (x) x)))
  2414. Í      (eq? p p))                ==>  #tˇ
  2415.  
  2416. ˘Rationale:ˇ It will usually be possible to implement Íeq?ˇ much more efficiently than Íeqv?ˇ, for
  2417. example, as a simple pointer comparison instead of as some more complicated operation. One
  2418. reason is that it may not be possible to compute Íeqv?ˇ of two numbers in constant time,
  2419. whereas Íeq?ˇ implemented as pointer comparison will always finish in constant time. ÍEq?ˇ
  2420. may be used like Íeqv?ˇ in applications using procedures to implement objects with state since
  2421. it obeys the same constraints as Íeqv?ˇ.
  2422.  
  2423.  
  2424. Í(equal?˘ obj¯1˘ obj¯2Í)                                      ˇessential procedure
  2425.  
  2426. ÍEqual?ˇ recursively compares the contents of pairs, vectors, and strings, applying Íeqv?ˇ on
  2427. other objects such as numbers and symbols. A rule of thumb is that objects are generally
  2428. Íequal?ˇ if they print the same. ÍEqual?ˇ may fail to terminate if its arguments are circular
  2429. data structures.
  2430.  
  2431. Í    (equal? 'a 'a)              ==>  #t
  2432. Í    (equal? '(a) '(a))          ==>  #t
  2433. Í    (equal? '(a (b) c)
  2434. Í            '(a (b) c))         ==>  #t
  2435. Í    (equal? "abc" "abc")        ==>  #t
  2436. Í    (equal? 2 2)                ==>  #t
  2437. Í    (equal? (make-vector 5 'a)
  2438. Í            (make-vector 5 'a)) ==>  #t
  2439. Í    (equal? (lambda (x) x)
  2440. Í            (lambda (y) y))     ==>  ˘unspecifiedˇ
  2441.  
  2442.  
  2443. Û6.3. Pairs and listsˇ
  2444.  
  2445. A ˘pairˇ (sometimes called a ˘dotted pairˇ) is a record structure with two fields called the car
  2446. and cdr fields (for historical reasons). Pairs are created by the procedure Íconsˇ. The car and
  2447. cdr fields are accessed by the procedures Ícarˇ and Ícdrˇ. The car and cdr fields are assigned
  2448. by the procedures Íset-car!ˇ and Íset-cdr!ˇ.
  2449.  
  2450. Pairs are used primarily to represent lists. A list can be defined recursively as either the
  2451. empty list or a pair whose cdr is a list. More precisely, the set of lists is defined as the
  2452. smallest set ˘Xˇ such that
  2453.  
  2454. • The empty list is in ˘Xˇ.
  2455.  
  2456. • If ˘listˇ is in ˘Xˇ, then any pair whose cdr field contains ˘listˇ is also in ˘Xˇ.
  2457.  
  2458. The objects in the car fields of successive pairs of a list are the elements of the list. For
  2459. example, a two-element list is a pair whose car is the first element and whose cdr is a pair
  2460. whose car is the second element and whose cdr is the empty list. The length of a list is the
  2461. number of elements, which is the same as the number of pairs.
  2462.  
  2463. The empty list is a special object of its own type (it is not a pair); it has no elements and its
  2464. length is zero.
  2465.  
  2466. ˘Note:ˇ The above definitions imply that all lists have finite length and are terminated by the
  2467. empty list.
  2468.  
  2469. The most general notation (external representation) for Scheme pairs is the ``dotted''
  2470. notation Í(˘c¯1Í . ˘c¯2Í)ˇ where ˘c¯1ˇ is the value of the car field and ˘c¯2ˇ is the value of the cdr
  2471. field. For example Í(4 . 5)ˇ is a pair whose car is 4 and whose cdr is 5. Note that Í(4 . 5)ˇ
  2472. is the external representation of a pair, not an expression that evaluates to a pair.
  2473.  
  2474. A more streamlined notation can be used for lists: the elements of the list are simply
  2475. enclosed in parentheses and separated by spaces. The empty list is written Í()ˇ. For example,
  2476.  
  2477. Í    (a b c d e)ˇ
  2478.  
  2479. and
  2480.  
  2481. Í    (a . (b . (c . (d . (e . ())))))ˇ
  2482.  
  2483. are equivalent notations for a list of symbols.
  2484.  
  2485. A chain of pairs not ending in the empty list is called an ˘improper listˇ. Note that an improper
  2486. list is not a list. The list and dotted notations can be combined to represent improper lists:
  2487.  
  2488. Í    (a b c . d)ˇ
  2489.  
  2490. is equivalent to
  2491.  
  2492. Í    (a . (b . (c . d)))ˇ
  2493.  
  2494. Whether a given pair is a list depends upon what is stored in the cdr field. When the
  2495. Íset-cdr!ˇ procedure is used, an object can be a list one moment and not the next:
  2496.  
  2497. Í    (define x (list 'a 'b 'c))
  2498. Í    (define y x)
  2499. Í    y                           ==>  (a b c)
  2500. Í    (list? y)                   ==>  #t
  2501. Í    (set-cdr! x 4)              ==>  ˘unspecifiedÍ
  2502. Í    x                           ==>  (a . 4)
  2503. Í    (eqv? x y)                  ==>  #t
  2504. Í    y                           ==>  (a . 4)
  2505. Í    (list? y)                   ==>  #f
  2506. Í    (set-cdr! x x)              ==>  ˘unspecifiedÍ
  2507. Í    (list? x)                   ==>  #fˇ
  2508.  
  2509. Within literal expressions and representations of objects read by the Íreadˇ procedure, the
  2510. forms Í'ˇ<datum>, Í`ˇ<datum>, Í,ˇ<datum>, and Í,@ˇ<datum> denote two-element lists whose first
  2511. elements are the symbols Íquoteˇ, Íquasiquoteˇ, Íunquoteˇ, and Íunquote-splicingˇ,
  2512. respectively. The second element in each case is <datum>. This convention is supported so
  2513. that arbitrary Scheme programs may be represented as lists. That is, according to Scheme's
  2514. grammar, every <expression> is also a <datum> (see section 7.1.2). Among other things,
  2515. this permits the use of the Íreadˇ procedure to parse Scheme programs. See section 3.3.
  2516.  
  2517.  
  2518. Í(pair?˘ objÍ)                                            ˇessential procedure
  2519.  
  2520. ÍPair?ˇ returns Í#tˇ if ˘objˇ is a pair, and otherwise returns Í#fˇ.
  2521.  
  2522. Í    (pair? '(a . b))            ==>  #t
  2523. Í    (pair? '(a b c))            ==>  #t
  2524. Í    (pair? '())                 ==>  #f
  2525. Í    (pair? '#(a b))             ==>  #fˇ
  2526.  
  2527.  
  2528. Í(cons˘ obj¯1˘ obj¯2Í)                                        ˇessential procedure
  2529.  
  2530. Returns a newly allocated pair whose car is ˘obj¯1ˇ and whose cdr is ˘obj¯2ˇ. The pair is
  2531. guaranteed to be different (in the sense of Íeqv?ˇ) from every existing object.
  2532.  
  2533. Í    (cons 'a '())               ==>  (a)
  2534. Í    (cons '(a) '(b c d))        ==>  ((a) b c d)
  2535. Í    (cons "a" '(b c))           ==>  ("a" b c)
  2536. Í    (cons 'a 3)                 ==>  (a . 3)
  2537. Í    (cons '(a b) 'c)            ==>  ((a b) . c)ˇ
  2538.  
  2539.  
  2540. Í(car˘ pairÍ)                                             ˇessential procedure
  2541.  
  2542. Returns the contents of the car field of ˘pairˇ. Note that it is an error to take the car of the
  2543. empty list.
  2544.  
  2545. Í    (car '(a b c))              ==>  a
  2546. Í    (car '((a) b c d))          ==>  (a)
  2547. Í    (car '(1 . 2))              ==>  1
  2548. Í    (car '())                   ==>  ˘errorˇ
  2549.  
  2550.  
  2551. Í(cdr˘ pairÍ)                                             ˇessential procedure
  2552.  
  2553. Returns the contents of the cdr field of ˘pairˇ. Note that it is an error to take the cdr of the
  2554. empty list.
  2555.  
  2556. Í    (cdr '((a) b c d))          ==>  (b c d)
  2557. Í    (cdr '(1 . 2))              ==>  2
  2558. Í    (cdr '())                   ==>  ˘errorˇ
  2559.  
  2560.  
  2561. Í(set-car!˘ pair objÍ)                                      ˇessential procedure
  2562.  
  2563. Stores ˘objˇ in the car field of ˘pairˇ. The value returned by Íset-car!ˇ is unspecified.
  2564.  
  2565. Í    (define (f) (list 'not-a-constant-list))
  2566. Í    (define (g) '(constant-list))
  2567. Í    (set-car! (f) 3)            ==>  ˘unspecifiedÍ
  2568. Í    (set-car! (g) 3)            ==>  ˘errorˇ
  2569.  
  2570.  
  2571. Í(set-cdr!˘ pair objÍ)                                      ˇessential procedure
  2572.  
  2573. Stores ˘objˇ in the cdr field of ˘pairˇ. The value returned by Íset-cdr!ˇ is unspecified.
  2574.  
  2575.  
  2576. Í(caar˘ pairÍ)                                            ˇessential procedure
  2577. Í(cadr˘ pairÍ)                                            ˇessential procedure
  2578.      .
  2579.      .
  2580.      .
  2581. Í(cdddar˘ pairÍ)                                          ˇessential procedure
  2582. Í(cddddr˘ pairÍ)                                          ˇessential procedure
  2583.  
  2584. These procedures are compositions of Ícarˇ and Ícdrˇ, where for example Ícaddrˇ could be
  2585. defined by
  2586.  
  2587. Í    (define caddr (lambda (x) (car (cdr (cdr x))))).ˇ
  2588.  
  2589. Arbitrary compositions, up to four deep, are provided. There are twenty-eight of these
  2590. procedures in all.
  2591.  
  2592.  
  2593. Í(null?˘ objÍ)                                            ˇessential procedure
  2594.  
  2595. Returns Í#tˇ if ˘objˇ is the empty list, otherwise returns Í#fˇ.
  2596.  
  2597.  
  2598. Í(list?˘ objÍ)                                            ˇessential procedure
  2599.  
  2600. Returns Í#tˇ if ˘objˇ is a list, otherwise returns Í#fˇ. By definition, all lists have finite length
  2601. and are terminated by the empty list.
  2602.  
  2603. Í    (list? '(a b c))            ==>  #t
  2604. Í    (list? '())                 ==>  #t
  2605. Í    (list? '(a . b))            ==>  #f
  2606. Í    (let ((x (list 'a)))
  2607. Í      (set-cdr! x x)
  2608. Í      (list? x))                ==>  #fˇ
  2609.  
  2610.  
  2611. Í(list˘ obj ...Í)                                           ˇessential procedure
  2612.  
  2613. Returns a newly allocated list of its arguments.
  2614.  
  2615. Í    (list 'a (+ 3 4) 'c)        ==>  (a 7 c)
  2616. Í    (list)                      ==>  ()ˇ
  2617.  
  2618.  
  2619. Í(length˘ listÍ)                                           ˇessential procedure
  2620.  
  2621. Returns the length of ˘listˇ.
  2622.  
  2623. Í    (length '(a b c))           ==>  3
  2624. Í    (length '(a (b) (c d e)))   ==>  3
  2625. Í    (length '())                ==>  0ˇ
  2626.  
  2627.  
  2628. Í(append˘ list ...Í)                                         ˇessential procedure
  2629.  
  2630. Returns a list consisting of the elements of the first ˘listˇ followed by the elements of the
  2631. other ˘listˇs.
  2632.  
  2633. Í    (append '(x) '(y))          ==>  (x y)
  2634. Í    (append '(a) '(b c d))      ==>  (a b c d)
  2635. Í    (append '(a (b)) '((c)))    ==>  (a (b) (c))ˇ
  2636.  
  2637. The resulting list is always newly allocated, except that it shares structure with the last
  2638. ˘listˇ argument. The last argument may actually be any object; an improper list results if the
  2639. last argument is not a proper list.
  2640.  
  2641. Í    (append '(a b) '(c . d))    ==>  (a b c . d)
  2642. Í    (append '() 'a)             ==>  aˇ
  2643.  
  2644.  
  2645. Í(reverse˘ listÍ)                                          ˇessential procedure
  2646.  
  2647. Returns a newly allocated list consisting of the elements of ˘listˇ in reverse order.
  2648.  
  2649. Í    (reverse '(a b c))          ==>  (c b a)
  2650. Í    (reverse '(a (b c) d (e (f))))  
  2651. Í              ==>  ((e (f)) d (b c) a)ˇ
  2652.  
  2653.  
  2654. Í(list-tail˘ list kÍ)                                              ˇprocedure
  2655.  
  2656. Returns the sublist of ˘listˇ obtained by omitting the first ˘kˇ elements. ÍList-tailˇ could be
  2657. defined by
  2658.  
  2659. Í    (define list-tail
  2660. Í      (lambda (x k)
  2661. Í        (if (zero? k)
  2662. Í            x
  2663. Í            (list-tail (cdr x) (- k 1)))))ˇ
  2664.  
  2665.  
  2666. Í(list-ref˘ list kÍ)                                        ˇessential procedure
  2667.  
  2668. Returns the ˘kˇth element of ˘listˇ. (This is the same as the car of Í(list-tail ˘listÍ ˘kÍ)ˇ.)
  2669.  
  2670. Í    (list-ref '(a b c d) 2)     ==>  c
  2671. Í    (list-ref '(a b c d)
  2672. Í              (inexact->exact (round 1.8))) 
  2673. Í              ==>  cˇ
  2674.  
  2675.  
  2676. Í(memq˘ obj listÍ)                                          ˇessential procedure
  2677. Í(memv˘ obj listÍ)                                          ˇessential procedure
  2678. Í(member˘ obj listÍ)                                        ˇessential procedure
  2679.  
  2680. These procedures return the first sublist of ˘listˇ whose car is ˘objˇ, where the sublists of ˘listˇ
  2681. are the non-empty lists returned by Í(list-tail ˘listÍ ˘kÍ)ˇ for ˘kˇ less than the length of ˘listˇ.
  2682. If ˘objˇ does not occur in ˘listˇ, then Í#fˇ (not the empty list) is returned. ÍMemqˇ uses Íeq?ˇ to
  2683. compare ˘objˇ with the elements of ˘listˇ, while Ímemvˇ uses Íeqv?ˇ and Ímemberˇ uses Íequal?ˇ.
  2684.  
  2685. Í    (memq 'a '(a b c))          ==>  (a b c)
  2686. Í    (memq 'b '(a b c))          ==>  (b c)
  2687. Í    (memq 'a '(b c d))          ==>  #f
  2688. Í    (memq (list 'a) '(b (a) c)) ==>  #f
  2689. Í    (member (list 'a)
  2690. Í            '(b (a) c))         ==>  ((a) c)
  2691. Í    (memq 101 '(100 101 102))   ==>  ˘unspecifiedÍ
  2692. Í    (memv 101 '(100 101 102))   ==>  (101 102)ˇ
  2693.  
  2694.  
  2695. Í(assq˘ obj alistÍ)                                         ˇessential procedure
  2696. Í(assv˘ obj alistÍ)                                         ˇessential procedure
  2697. Í(assoc˘ obj alistÍ)                                        ˇessential procedure
  2698.  
  2699. ˘Alistˇ (for ``association list'') must be a list of pairs. These procedures find the first pair in
  2700. ˘alistˇ whose car field is ˘objˇ, and returns that pair. If no pair in ˘alistˇ has ˘objˇ as its car, then
  2701. Í#fˇ (not the empty list) is returned. ÍAssqˇ uses Íeq?ˇ to compare ˘objˇ with the car fields of the
  2702. pairs in ˘alistˇ, while Íassvˇ uses Íeqv?ˇ and Íassocˇ uses Íequal?ˇ.
  2703.  
  2704. Í    (define e '((a 1) (b 2) (c 3)))
  2705. Í    (assq 'a e)                 ==>  (a 1)
  2706. Í    (assq 'b e)                 ==>  (b 2)
  2707. Í    (assq 'd e)                 ==>  #f
  2708. Í    (assq (list 'a) '(((a)) ((b)) ((c))))
  2709. Í                                ==>  #f
  2710. Í    (assoc (list 'a) '(((a)) ((b)) ((c))))   
  2711. Í                                ==>  ((a))
  2712. Í    (assq 5 '((2 3) (5 7) (11 13)))    
  2713. Í                                ==>  ˘unspecifiedÍ
  2714. Í    (assv 5 '((2 3) (5 7) (11 13)))    
  2715. Í                                ==>  (5 7)ˇ
  2716.  
  2717. ˘Rationale:ˇ Although they are ordinarily used as predicates, Ímemqˇ, Ímemvˇ, Ímemberˇ, Íassqˇ,
  2718. Íassvˇ, and Íassocˇ do not have question marks in their names because they return useful
  2719. values rather than just Í#tˇ or Í#fˇ.
  2720.  
  2721.  
  2722. Û6.4. Symbolsˇ
  2723.  
  2724. Symbols are objects whose usefulness rests on the fact that two symbols are identical (in
  2725. the sense of Íeqv?ˇ) if and only if their names are spelled the same way. This is exactly the
  2726. property needed to represent identifiers in programs, and so most implementations of
  2727. Scheme use them internally for that purpose. Symbols are useful for many other
  2728. applications; for instance, they may be used the way enumerated values are used in Pascal.
  2729.  
  2730. The rules for writing a symbol are exactly the same as the rules for writing an identifier;
  2731. see sections 2.1 and 7.1.1.
  2732.  
  2733. It is guaranteed that any symbol that has been returned as part of a literal expression, or
  2734. read using the Íreadˇ procedure, and subsequently written out using the Íwriteˇ procedure,
  2735. will read back in as the identical symbol (in the sense of Íeqv?ˇ). The Ístring->symbolˇ
  2736. procedure, however, can create symbols for which this write/read invariance may not hold
  2737. because their names contain special characters or letters in the non-standard case.
  2738.  
  2739. ˘Note:ˇ Some implementations of Scheme have a feature known as ``slashification'' in order to
  2740. guarantee write/read invariance for all symbols, but historically the most important use of
  2741. this feature has been to compensate for the lack of a string data type.
  2742.  
  2743. Some implementations also have ``uninterned symbols'', which defeat write/read invariance
  2744. even in implementations with slashification, and also generate exceptions to the rule that
  2745. two symbols are the same if and only if their names are spelled the same.
  2746.  
  2747.  
  2748. Í(symbol?˘ objÍ)                                          ˇessential procedure
  2749.  
  2750. Returns Í#tˇ if ˘objˇ is a symbol, otherwise returns Í#fˇ.
  2751.  
  2752. Í    (symbol? 'foo)              ==>  #t
  2753. Í    (symbol? (car '(a b)))      ==>  #t
  2754. Í    (symbol? "bar")             ==>  #f
  2755. Í    (symbol? 'nil)              ==>  #t
  2756. Í    (symbol? '())               ==>  #f
  2757. Í    (symbol? #f)                ==>  #fˇ
  2758.  
  2759.  
  2760. Í(symbol->string˘ symbolÍ)                                ˇessential procedure
  2761.  
  2762. Returns the name of ˘symbolˇ as a string. If the symbol was part of an object returned as the
  2763. value of a literal expression (section 4.1.2) or by a call to the Íreadˇ procedure, and its name
  2764. contains alphabetic characters, then the string returned will contain characters in the
  2765. implementation's preferred standard case; some implementations will prefer upper case,
  2766. others lower case. If the symbol was returned by Ístring->symbolˇ, the case of characters
  2767. in the string returned will be the same as the case in the string that was passed to
  2768. Ístring->symbolˇ. It is an error to apply mutation procedures like Ístring-set!ˇ to strings
  2769. returned by this procedure.
  2770.  
  2771. The following examples assume that the implementation's standard case is lower case:
  2772.  
  2773. Í    (symbol->string 'flying-fish)
  2774. Í                                ==>  "flying-fish"
  2775. Í    (symbol->string 'Martin)    ==>  "martin"
  2776. Í    (symbol->string
  2777. Í       (string->symbol "Malvina"))
  2778. Í                                ==>  "Malvina"ˇ
  2779.  
  2780.  
  2781. Í(string->symbol˘ stringÍ)                                 ˇessential procedure
  2782.  
  2783. Returns the symbol whose name is ˘stringˇ. This procedure can create symbols with names
  2784. containing special characters or letters in the non-standard case, but it is usually a bad idea
  2785. to create such symbols because in some implementations of Scheme they cannot be read as
  2786. themselves. See Ísymbol->stringˇ.
  2787.  
  2788. The following examples assume that the implementation's standard case is lower case:
  2789.  
  2790. Í    (eq? 'mISSISSIppi 'mississippi)  
  2791. Í              ==>  #t
  2792. Í    (string->symbol "mISSISSIppi")  
  2793. Í              ==>  ˇthe symbol with nameÍ "mISSISSIppi"
  2794. Í    (eq? 'bitBlt (string->symbol "bitBlt"))     
  2795. Í              ==>  #f
  2796. Í    (eq? 'JollyWog
  2797. Í         (string->symbol
  2798. Í           (symbol->string 'JollyWog)))  
  2799. Í              ==>  #t
  2800. Í    (string=? "K. Harper, M.D."
  2801. Í              (symbol->string
  2802. Í                (string->symbol "K. Harper, M.D.")))  
  2803. Í              ==>  #tˇ
  2804.  
  2805.  
  2806. Û6.5. Numbersˇ
  2807.  
  2808. Numerical computation has traditionally been neglected by the Lisp community. Until Common
  2809. Lisp there was no carefully thought out strategy for organizing numerical computation, and
  2810. with the exception of the MacLisp system [Pitman] little effort was made to execute
  2811. numerical code efficiently. This report recognizes the excellent work of the Common Lisp
  2812. committee and accepts many of their recommendations. In some ways this report simplifies
  2813. and generalizes their proposals in a manner consistent with the purposes of Scheme.
  2814.  
  2815. It is important to distinguish between the mathematical numbers, the Scheme numbers that
  2816. attempt to model them, the machine representations used to implement the Scheme numbers,
  2817. and notations used to write numbers. This report uses the types ˘numberˇ, ˘complexˇ, ˘realˇ,
  2818. ˘rationalˇ, and ˘integerˇ to refer to both mathematical numbers and Scheme numbers. Machine
  2819. representations such as fixed point and floating point are referred to by names such as
  2820. ˘fixnumˇ and ˘flonumˇ.
  2821.  
  2822.  
  2823. ¸6.5.1. Numerical typesˇ
  2824.  
  2825. Mathematically, numbers may be arranged into a tower of subtypes in which each level is a
  2826. subset of the level above it:
  2827.  
  2828.             number 
  2829.             complex 
  2830.             real 
  2831.             rational 
  2832.             integer
  2833.  
  2834. For example, 3 is an integer. Therefore 3 is also a rational, a real, and a complex. The same
  2835. is true of the Scheme numbers that model 3. For Scheme numbers, these types are defined
  2836. by the predicates Ínumber?ˇ, Ícomplex?ˇ, Íreal?ˇ, Írational?ˇ, and Íinteger?ˇ.
  2837.  
  2838. There is no simple relationship between a number's type and its representation inside a
  2839. computer. Although most implementations of Scheme will offer at least two different
  2840. representations of 3, these different representations denote the same integer.
  2841.  
  2842. Scheme's numerical operations treat numbers as abstract data, as independent of their
  2843. representation as possible. Although an implementation of Scheme may use fixnum, flonum,
  2844. and perhaps other representations for numbers, this should not be apparent to a casual
  2845. programmer writing simple programs.
  2846.  
  2847. It is necessary, however, to distinguish between numbers that are represented exactly and
  2848. those that may not be. For example, indexes into data structures must be known exactly, as
  2849. must some polynomial coefficients in a symbolic algebra system. On the other hand, the
  2850. results of measurements are inherently inexact, and irrational numbers may be
  2851. approximated by rational and therefore inexact approximations. In order to catch uses of
  2852. inexact numbers where exact numbers are required, Scheme explicitly distinguishes exact
  2853. from inexact numbers. This distinction is orthogonal to the dimension of type.
  2854.  
  2855.  
  2856. ¸6.5.2. Exactnessˇ
  2857.  
  2858. Scheme numbers are either ˘exactˇ or ˘inexactˇ. A number is exact if it was written as an exact
  2859. constant or was derived from exact numbers using only exact operations. A number is
  2860. inexact if it was written as an inexact constant, if it was derived using inexact ingredients,
  2861. or if it was derived using inexact operations. Thus inexactness is a contagious property of a
  2862. number.
  2863.  
  2864. If two implementations produce exact results for a computation that did not involve inexact
  2865. intermediate results, the two ultimate results will be mathematically equivalent. This is
  2866. generally not true of computations involving inexact numbers since approximate methods
  2867. such as floating point arithmetic may be used, but it is the duty of each implementation to
  2868. make the result as close as practical to the mathematically ideal result.
  2869.  
  2870. Rational operations such as Í+ˇ should always produce exact results when given exact
  2871. arguments. If the operation is unable to produce an exact result, then it may either report
  2872. the violation of an implementation restriction or it may silently coerce its result to an
  2873. inexact value. See section 6.5.3.
  2874.  
  2875. With the exception of Íinexact->exactˇ, the operations described in this section must
  2876. generally return inexact results when given any inexact arguments. An operation may,
  2877. however, return an exact result if it can prove that the value of the result is unaffected by
  2878. the inexactness of its arguments. For example, multiplication of any number by an exact
  2879. zero may produce an exact zero result, even if the other argument is inexact.
  2880.  
  2881.  
  2882. ¸6.5.3. Implementation restrictionsˇ
  2883.  
  2884. Implementations of Scheme are not required to implement the whole tower of subtypes given
  2885. in section 6.5.1, but they must implement a coherent subset consistent with both the
  2886. purposes of the implementation and the spirit of the Scheme language. For example, an
  2887. implementation in which all numbers are real may still be quite useful.
  2888.  
  2889. Implementations may also support only a limited range of numbers of any type, subject to
  2890. the requirements of this section. The supported range for exact numbers of any type may be
  2891. different from the supported range for inexact numbers of that type. For example, an
  2892. implementation that uses flonums to represent all its inexact real numbers may support a
  2893. practically unbounded range of exact integers and rationals while limiting the range of
  2894. inexact reals (and therefore the range of inexact integers and rationals) to the dynamic
  2895. range of the flonum format. Furthermore the gaps between the representable inexact
  2896. integers and rationals are likely to be very large in such an implementation as the limits of
  2897. this range are approached.
  2898.  
  2899. An implementation of Scheme must support exact integers throughout the range of numbers
  2900. that may be used for indexes of lists, vectors, and strings or that may result from
  2901. computing the length of a list, vector, or string. The Ílengthˇ, Ívector-lengthˇ, and
  2902. Ístring-lengthˇ procedures must return an exact integer, and it is an error to use anything
  2903. but an exact integer as an index. Furthermore any integer constant within the index range, if
  2904. expressed by an exact integer syntax, will indeed be read as an exact integer, regardless of
  2905. any implementation restrictions that may apply outside this range. Finally, the procedures
  2906. listed below will always return an exact integer result provided all their arguments are
  2907. exact integers and the mathematically expected result is representable as an exact integer
  2908. within the implementation:
  2909.  
  2910. Í    +            -             *
  2911. Í    quotient     remainder     modulo
  2912. Í    max          min           abs
  2913. Í    numerator    denominator   gcd
  2914. Í    lcm          floor         ceiling
  2915. Í    truncate     round         rationalize
  2916. Í    exptˇ
  2917.  
  2918. Implementations are encouraged, but not required, to support exact integers and exact
  2919. rationals of practically unlimited size and precision, and to implement the above procedures
  2920. and the Í/ˇ procedure in such a way that they always return exact results when given exact
  2921. arguments. If one of these procedures is unable to deliver an exact result when given exact
  2922. arguments, then it may either report a violation of an implementation restriction or it may
  2923. silently coerce its result to an inexact number. Such a coercion may cause an error later.
  2924.  
  2925. An implementation may use floating point and other approximate representation strategies
  2926. for inexact numbers. This report recommends, but does not require, that the IEEE 32-bit and
  2927. 64-bit floating point standards be followed by implementations that use flonum
  2928. representations, and that implementations using other representations should match or
  2929. exceed the precision achievable using these floating point standards [IEEE].
  2930.  
  2931. In particular, implementations that use flonum representations must follow these rules: A
  2932. flonum result must be represented with at least as much precision as is used to express any
  2933. of the inexact arguments to that operation. It is desirable (but not required) for potentially
  2934. inexact operations such as Ísqrtˇ, when applied to exact arguments, to produce exact
  2935. answers whenever possible (for example the square root of an exact 4 ought to be an exact
  2936. 2). If, however, an exact number is operated upon so as to produce an inexact result (as by
  2937. Ísqrtˇ), and if the result is represented as a flonum, then the most precise flonum format
  2938. available must be used; but if the result is represented in some other way then the
  2939. representation must have at least as much precision as the most precise flonum format
  2940. available.
  2941.  
  2942. Although Scheme allows a variety of written notations for numbers, any particular
  2943. implementation may support only some of them. For example, an implementation in which all
  2944. numbers are real need not support the rectangular and polar notations for complex numbers.
  2945. If an implementation encounters an exact numerical constant that it cannot represent as an
  2946. exact number, then it may either report a violation of an implementation restriction or it
  2947. may silently represent the constant by an inexact number.
  2948.  
  2949.  
  2950. ¸6.5.4. Syntax of numerical constantsˇ
  2951.  
  2952. The syntax of the written representations for numbers is described formally in section
  2953. 7.1.1.
  2954.  
  2955. A number may be written in binary, octal, decimal, or hexadecimal by the use of a radix
  2956. prefix. The radix prefixes are Í#bˇ (binary), Í#oˇ (octal), Í#dˇ (decimal), and Í#xˇ (hexadecimal).
  2957. With no radix prefix, a number is assumed to be expressed in decimal.
  2958.  
  2959. A numerical constant may be specified to be either exact or inexact by a prefix. The
  2960. prefixes are Í#eˇ for exact, and Í#iˇ for inexact. An exactness prefix may appear before or
  2961. after any radix prefix that is used. If the written representation of a number has no
  2962. exactness prefix, the constant may be either inexact or exact. It is inexact if it contains a
  2963. decimal point, an exponent, or a ``Í#ˇ'' character in the place of a digit, otherwise it is exact.
  2964.  
  2965. In systems with inexact numbers of varying precisions it may be useful to specify the
  2966. precision of a constant. For this purpose, numerical constants may be written with an
  2967. exponent marker that indicates the desired precision of the inexact representation. The
  2968. letters Ísˇ, Ífˇ, Ídˇ, and Ílˇ specify the use of ˘shortˇ, ˘singleˇ, ˘doubleˇ, and ˘longˇ precision,
  2969. respectively. (When fewer than four internal inexact representations exist, the four size
  2970. specifications are mapped onto those available. For example, an implementation with two
  2971. internal representations may map short and single together and long and double together.) In
  2972. addition, the exponent marker Íeˇ specifies the default precision for the implementation. The
  2973. default precision has at least as much precision as ˘doubleˇ, but implementations may wish to
  2974. allow this default to be set by the user.
  2975.  
  2976. Í    3.14159265358979F0
  2977. Í           ˇRound to single:Í 3.141593
  2978. Í    0.6L0
  2979. Í           ˇExtend to long:Í .600000000000000ˇ
  2980.  
  2981.  
  2982. ¸6.5.5. Numerical operationsˇ
  2983.  
  2984. The reader is referred to section 1.3.3 for a summary of the naming conventions used to
  2985. specify restrictions on the types of arguments to numerical routines. The examples used in
  2986. this section assume that any numerical constant written using an exact notation is indeed
  2987. represented as an exact number. Some examples also assume that certain numerical
  2988. constants written using an inexact notation can be represented without loss of accuracy; the
  2989. inexact constants were chosen so that this is likely to be true in implementations that use
  2990. flonums to represent inexact numbers.
  2991.  
  2992.  
  2993. Í(number?˘ objÍ)                                          ˇessential procedure
  2994. Í(complex?˘ objÍ)                                         ˇessential procedure
  2995. Í(real?˘ objÍ)                                            ˇessential procedure
  2996. Í(rational?˘ objÍ)                                        ˇessential procedure
  2997. Í(integer?˘ objÍ)                                         ˇessential procedure
  2998.  
  2999. These numerical type predicates can be applied to any kind of argument, including
  3000. non-numbers. They return Í#tˇ if the object is of the named type, and otherwise they return
  3001. Í#fˇ. In general, if a type predicate is true of a number then all higher type predicates are also
  3002. true of that number. Consequently, if a type predicate is false of a number, then all lower
  3003. type predicates are also false of that number.
  3004.  
  3005. If ˘zˇ is an inexact complex number, then Í(real? ˘zÍ)ˇ is true if and only if Í(zero?
  3006. Í(imag-part ˘zÍ))ˇ is true. If ˘xˇ is an inexact real number, then Í(integer? ˘xÍ)ˇ is true if and
  3007. only if Í(= ˘xÍ (round ˘xÍ))ˇ.
  3008.  
  3009. Í    (complex? 3+4i)             ==>  #t
  3010. Í    (complex? 3)                ==>  #t
  3011. Í    (real? 3)                   ==>  #t
  3012. Í    (real? -2.5+0.0i)           ==>  #t
  3013. Í    (real? #e1e10)              ==>  #t
  3014. Í    (rational? 6/10)            ==>  #t
  3015. Í    (rational? 6/3)             ==>  #t
  3016. Í    (integer? 3+0i)             ==>  #t
  3017. Í    (integer? 3.0)              ==>  #t
  3018. Í    (integer? 8/4)              ==>  #tˇ
  3019.  
  3020. ˘Note:ˇ The behavior of these type predicates on inexact numbers is unreliable, since any
  3021. inaccuracy may affect the result.
  3022.  
  3023. ˘Note:ˇ In many implementations the Írational?ˇ procedure will be the same as Íreal?ˇ, and
  3024. the Ícomplex?ˇ procedure will be the same as Ínumber?ˇ, but unusual implementations may be
  3025. able to represent some irrational numbers exactly or may extend the number system to
  3026. support some kind of non-complex numbers.
  3027.  
  3028.  
  3029. Í(exact?˘ zÍ)                                             ˇessential procedure
  3030. Í(inexact?˘ zÍ)                                           ˇessential procedure
  3031.  
  3032. These numerical predicates provide tests for the exactness of a quantity. For any Scheme
  3033. number, precisely one of these predicates is true.
  3034.  
  3035.  
  3036. Í(=˘ z¯1˘ z¯2˘ z¯3˘ ...Í)                                          ˇessential procedure
  3037. Í(<˘ x¯1˘ x¯2˘ x¯3˘ ...Í)                                         ˇessential procedure
  3038. Í(>˘ x¯1˘ x¯2˘ x¯3˘ ...Í)                                         ˇessential procedure
  3039. Í(<=˘ x¯1˘ x¯2˘ x¯3˘ ...Í)                                        ˇessential procedure
  3040. Í(>=˘ x¯1˘ x¯2˘ x¯3˘ ...Í)                                        ˇessential procedure
  3041.  
  3042. These procedures return Í#tˇ if their arguments are (respectively): equal, monotonically
  3043. increasing, monotonically decreasing, monotonically nondecreasing, or monotonically
  3044. nonincreasing.
  3045.  
  3046. These predicates are required to be transitive.
  3047.  
  3048. ˘Note:ˇ The traditional implementations of these predicates in Lisp-like languages are not
  3049. transitive.
  3050.  
  3051. ˘Note:ˇ While it is not an error to compare inexact numbers using these predicates, the results
  3052. may be unreliable because a small inaccuracy may affect the result; this is especially true of
  3053. Í=ˇ and Ízero?ˇ. When in doubt, consult a numerical analyst.
  3054.  
  3055.  
  3056. Í(zero?˘ zÍ)                                              ˇessential procedure
  3057. Í(positive?˘ xÍ)                                         ˇessential procedure
  3058. Í(negative?˘ xÍ)                                         ˇessential procedure
  3059. Í(odd?˘ nÍ)                                               ˇessential procedure
  3060. Í(even?˘ nÍ)                                              ˇessential procedure
  3061.  
  3062. These numerical predicates test a number for a particular property, returning Í#tˇ or Í#fˇ. See
  3063. note above.
  3064.  
  3065.  
  3066. Í(max˘ x¯1˘ x¯2˘ ...Í)                                          ˇessential procedure
  3067. Í(min˘ x¯1˘ x¯2˘ ...Í)                                          ˇessential procedure
  3068.  
  3069. These procedures return the maximum or minimum of their arguments.
  3070.  
  3071. Í    (max 3 4)                   ==>  4    ; exact
  3072. Í    (max 3.9 4)                 ==>  4.0  ; inexactˇ
  3073.  
  3074. ˘Note:ˇ If any argument is inexact, then the result will also be inexact (unless the procedure
  3075. can prove that the inaccuracy is not large enough to affect the result, which is possible only
  3076. in unusual implementations). If Íminˇ or Ímaxˇ is used to compare numbers of mixed exactness,
  3077. and the numerical value of the result cannot be represented as an inexact number without
  3078. loss of accuracy, then the procedure may report a violation of an implementation restriction.
  3079.  
  3080.  
  3081. Í(+˘ z¯1˘ ...Í)                                               ˇessential procedure
  3082. Í(*˘ z¯1˘ ...Í)                                               ˇessential procedure
  3083.  
  3084. These procedures return the sum or product of their arguments.
  3085.  
  3086. Í    (+ 3 4)                     ==>  7
  3087. Í    (+ 3)                       ==>  3
  3088. Í    (+)                         ==>  0
  3089. Í    (* 4)                       ==>  4
  3090. Í    (*)                         ==>  1ˇ
  3091.  
  3092.  
  3093. Í(-˘ z¯1˘ z¯2Í)                                              ˇessential procedure
  3094. Í(-˘ zÍ)                                                  ˇessential procedure
  3095. Í(-˘ z¯1˘ z¯2˘ ...Í)                                                   ˇprocedure
  3096. Í(/˘ z¯1˘ z¯2Í)                                              ˇessential procedure
  3097. Í(/˘ zÍ)                                                  ˇessential procedure
  3098. Í(/˘ z¯1˘ z¯2˘ ...Í)                                                   ˇprocedure
  3099.  
  3100. With two or more arguments, these procedures return the difference or quotient of their
  3101. arguments, associating to the left. With one argument, however, they return the additive or
  3102. multiplicative inverse of their argument.
  3103.  
  3104. Í    (- 3 4)                     ==>  -1
  3105. Í    (- 3 4 5)                   ==>  -6
  3106. Í    (- 3)                       ==>  -3
  3107. Í    (/ 3 4 5)                   ==>  3/20
  3108. Í    (/ 3)                       ==>  1/3ˇ
  3109.  
  3110.  
  3111. Í(abs˘ xÍ)                                               ˇessential procedure
  3112.  
  3113. ÍAbsˇ returns the magnitude of its argument.
  3114.  
  3115. Í    (abs -7)                    ==>  7ˇ
  3116.  
  3117.  
  3118. Í(quotient˘ n¯1˘ n¯2Í)                                       ˇessential procedure
  3119. Í(remainder˘ n¯1˘ n¯2Í)                                      ˇessential procedure
  3120. Í(modulo˘ n¯1˘ n¯2Í)                                         ˇessential procedure
  3121.  
  3122. These procedures implement number-theoretic (integer) division: For positive integers ˘n¯1ˇ
  3123. and ˘n¯2ˇ, if ˘n¯3ˇ and ˘n¯4ˇ are integers such that ˘n¯1ˇ=˘n¯2˘n¯3ˇ+˘n¯4ˇ and 0 <= ˘n¯4ˇ < ˘n¯2ˇ, then
  3124.  
  3125. Í    (quotient ˘n¯1Í ˘n¯2Í)            ==>  ˘n¯3Í
  3126. Í    (remainder ˘n¯1Í ˘n¯2Í)           ==>  ˘n¯4Í
  3127. Í    (modulo ˘n¯1Í ˘n¯2Í)              ==>  ˘n¯4ˇ
  3128.  
  3129. For integers ˘n¯1ˇ and ˘n¯2ˇ with ˘n¯2ˇ not equal to 0,
  3130.  
  3131. Í    (= ˘n¯1Í (+ (* ˘n¯2Í (quotient ˘n¯1Í ˘n¯2Í))
  3132. Í             (remainder ˘n¯1Í ˘n¯2Í)))
  3133. Í                                ==>  #tˇ
  3134.  
  3135. provided all numbers involved in that computation are exact.
  3136.  
  3137. The value returned by Íquotientˇ always has the sign of the product of its arguments.
  3138. ÍRemainderˇ and Ímoduloˇ differ on negative arguments; the Íremainderˇ is either zero or has
  3139. the sign of the dividend, while the Ímoduloˇ always has the sign of the divisor:
  3140.  
  3141. Í    (modulo 13 4)               ==>  1
  3142. Í    (remainder 13 4)            ==>  1
  3143. Í    
  3144. Í    (modulo -13 4)              ==>  3
  3145. Í    (remainder -13 4)           ==>  -1
  3146. Í    
  3147. Í    (modulo 13 -4)              ==>  -3
  3148. Í    (remainder 13 -4)           ==>  1
  3149. Í    
  3150. Í    (modulo -13 -4)             ==>  -1
  3151. Í    (remainder -13 -4)          ==>  -1
  3152. Í    
  3153. Í    (remainder -13 -4.0)        ==>  -1.0  ; inexactˇ
  3154.  
  3155.  
  3156. Í(gcd˘ n¯1˘ ...Í)                                             ˇessential procedure
  3157. Í(lcm˘ n¯1˘ ...Í)                                             ˇessential procedure
  3158.  
  3159. These procedures return the greatest common divisor or least common multiple of their
  3160. arguments. The result is always non-negative.
  3161.  
  3162. Í    (gcd 32 -36)                ==>  4
  3163. Í    (gcd)                       ==>  0
  3164. Í    (lcm 32 -36)                ==>  288
  3165. Í    (lcm 32.0 -36)              ==>  288.0  ; inexact
  3166. Í    (lcm)                       ==>  1ˇ
  3167.  
  3168.  
  3169. Í(numerator˘ qÍ)                                                 ˇprocedure
  3170. Í(denominator˘ qÍ)                                               ˇprocedure
  3171.  
  3172. These procedures return the numerator or denominator of their argument; the result is
  3173. computed as if the argument was represented as a fraction in lowest terms. The denominator
  3174. is always positive. The denominator of 0 is defined to be 1.
  3175.  
  3176. Í    (numerator (/ 6 4))         ==>  3
  3177. Í    (denominator (/ 6 4))       ==>  2
  3178. Í    (denominator
  3179. Í      (exact->inexact (/ 6 4))) ==> 2.0ˇ
  3180.  
  3181.  
  3182. Í(floor˘ xÍ)                                             ˇessential procedure
  3183. Í(ceiling˘ xÍ)                                           ˇessential procedure
  3184. Í(truncate˘ xÍ)                                          ˇessential procedure
  3185. Í(round˘ xÍ)                                             ˇessential procedure
  3186.  
  3187. These procedures return integers. ÍFloorˇ returns the largest integer not larger than ˘xˇ.
  3188. ÍCeilingˇ returns the smallest integer not smaller than ˘xˇ. ÍTruncateˇ returns the integer
  3189. closest to ˘xˇ whose absolute value is not larger than the absolute value of ˘xˇ. ÍRoundˇ returns
  3190. the closest integer to ˘xˇ, rounding to even when ˘xˇ is halfway between two integers.
  3191.  
  3192. ˘Rationale:ˇ ÍRoundˇ rounds to even for consistency with the default rounding mode specified by
  3193. the IEEE floating point standard.
  3194.  
  3195. ˘Note:ˇ If the argument to one of these procedures is inexact, then the result will also be
  3196. inexact. If an exact value is needed, the result should be passed to the Íinexact->exactˇ
  3197. procedure.
  3198.  
  3199. Í    (floor -4.3)                ==>  -5.0
  3200. Í    (ceiling -4.3)              ==>  -4.0
  3201. Í    (truncate -4.3)             ==>  -4.0
  3202. Í    (round -4.3)                ==>  -4.0
  3203. Í    
  3204. Í    (floor 3.5)                 ==>  3.0
  3205. Í    (ceiling 3.5)               ==>  4.0
  3206. Í    (truncate 3.5)              ==>  3.0
  3207. Í    (round 3.5)                 ==>  4.0  ; inexact
  3208. Í    
  3209. Í    (round 7/2)                 ==>  4    ; exact
  3210. Í    (round 7)                   ==>  7ˇ
  3211.  
  3212.  
  3213. Í(rationalize˘ x yÍ)                                             ˇprocedure
  3214.  
  3215. ÍRationalizeˇ returns the ˘simplestˇ rational number differing from ˘xˇ by no more than ˘yˇ. A
  3216. rational number ˘r¯1ˇ is ˘simplerˇ than another rational number ˘r¯2ˇ if ˘r¯1ˇ = ˘p¯1ˇ/˘q¯1ˇ and ˘r¯2ˇ = ˘p¯2ˇ/˘q¯2ˇ
  3217. (in lowest terms) and |˘p¯1ˇ| <= |˘p¯2ˇ| and |˘q¯1ˇ| <= |˘q¯2ˇ|. Thus 3/5 is simpler than 4/7. Although not
  3218. all rationals are comparable in this ordering (consider 2/7 and 3/5) any interval contains a
  3219. rational number that is simpler than every other rational number in that interval (the
  3220. simpler 2/5 lies between 2/7 and 3/5). Note that 0 = 0/1 is the simplest rational of all.
  3221.  
  3222. Í    (rationalize
  3223. Í      (inexact->exact .3) 1/10) ==> 1/3    ; exact
  3224. Í    (rationalize .3 1/10)       ==> #i1/3  ; inexactˇ
  3225.  
  3226.  
  3227. Í(exp˘ zÍ)                                                       ˇprocedure
  3228. Í(log˘ zÍ)                                                       ˇprocedure
  3229. Í(sin˘ zÍ)                                                       ˇprocedure
  3230. Í(cos˘ zÍ)                                                       ˇprocedure
  3231. Í(tan˘ zÍ)                                                       ˇprocedure
  3232. Í(asin˘ zÍ)                                                      ˇprocedure
  3233. Í(acos˘ zÍ)                                                      ˇprocedure
  3234. Í(atan˘ zÍ)                                                      ˇprocedure
  3235. Í(atan˘ y xÍ)                                                    ˇprocedure
  3236.  
  3237. These procedures are part of every implementation that supports general real numbers; they
  3238. compute the usual transcendental functions. ÍLogˇ computes the natural logarithm of ˘zˇ (not the
  3239. base ten logarithm). ÍAsinˇ, Íacosˇ, and Íatanˇ compute arcsine (sin˝-1ˇ), arccosine (cos˝-1ˇ), and
  3240. arctangent (tan˝-1ˇ), respectively. The two-argument variant of Íatanˇ computes Í(angle
  3241. Í(make-rectangular ˘xÍ ˘yÍ))ˇ (see below), even in implementations that don't support
  3242. general complex numbers.
  3243.  
  3244. In general, the mathematical functions log, arcsine, arccosine, and arctangent are multiply
  3245. defined. For nonzero real ˘xˇ, the value of log ˘xˇ is defined to be the one whose imaginary part
  3246. lies in the range -π (exclusive) to π (inclusive). log 0 is undefined. The value of log ˘zˇ when ˘zˇ
  3247. is complex is defined according to the formula
  3248.  
  3249.     log ˘zˇ = log magnitude(˘zˇ) + ˘iˇ angle(˘zˇ)
  3250.  
  3251. With log defined this way, the values of sin˝-1ˇ ˘zˇ, cos˝-1ˇ ˘zˇ, and tan˝-1ˇ ˘zˇ are according to the
  3252. following formulae:
  3253.  
  3254.     sin˝-1ˇ ˘zˇ = -˘iˇ log(˘izˇ + (1 - ˘z˜2ˇ)˝.5ˇ)
  3255.  
  3256.     cos˝-1ˇ ˘zˇ = π/2 - sin˝-1ˇ ˘zˇ
  3257.  
  3258.     tan˝-1ˇ ˘zˇ = (log (1 + ˘izˇ) - log (1-˘izˇ)) / (2˘iˇ)
  3259.  
  3260. The above specification follows [CLtL], which in turn cites [Penfield]; refer to these sources
  3261. for more detailed discussion of branch cuts, boundary conditions, and implementation of
  3262. these functions. When it is possible these procedures produce a real result from a real
  3263. argument.
  3264.  
  3265.  
  3266. Í(sqrt˘ zÍ)                                                      ˇprocedure
  3267.  
  3268. Returns the principal square root of ˘zˇ. The result will have either positive real part, or zero
  3269. real part and non-negative imaginary part.
  3270.  
  3271.  
  3272. Í(expt˘ z¯1˘ z¯2Í)                                                  ˇprocedure
  3273.  
  3274. Returns ˘z¯1ˇ raised to the power ˘z¯2ˇ:
  3275.  
  3276.     z1˝z2ˇ = e˝z2 log z1ˇ
  3277.  
  3278. 0˝0ˇ is defined to be equal to 1.
  3279.  
  3280.  
  3281. Í(make-rectangular˘ x¯1˘ x¯2Í)                                      ˇprocedure
  3282. Í(make-polar˘ x¯3˘ x¯4Í)                                            ˇprocedure
  3283. Í(real-part˘ zÍ)                                                 ˇprocedure
  3284. Í(imag-part˘ zÍ)                                                 ˇprocedure
  3285. Í(magnitude˘ zÍ)                                                 ˇprocedure
  3286. Í(angle˘ zÍ)                                                     ˇprocedure
  3287.  
  3288. These procedures are part of every implementation that supports general complex numbers.
  3289. Suppose ˘x¯1ˇ, ˘x¯2ˇ, ˘x¯3ˇ, and ˘x¯4ˇ are real numbers and ˘zˇ is a complex number such that
  3290.  
  3291.     ˘zˇ = ˘x¯1ˇ + ˘x¯2˘iˇ = ˘x¯3ˇ . e˝i x4ˇ
  3292.  
  3293. Then Ímake-rectangularˇ and Ímake-polarˇ return ˘zˇ, Íreal-partˇ returns ˘x¯1ˇ, Íimag-partˇ
  3294. returns ˘x¯2ˇ, Ímagnitudeˇ returns ˘x¯3ˇ, and Íangleˇ returns ˘x¯4ˇ. In the case of Íangleˇ, whose
  3295. value is not uniquely determined by the preceding rule, the value returned will be the one in
  3296. the range -π (exclusive) to π (inclusive).
  3297.  
  3298. ˘Rationale:ˇ ÍMagnitudeˇ is the same as Íabsˇ for a real argument, but Íabsˇ must be present in
  3299. all implementations, whereas Ímagnitudeˇ need only be present in implementations that
  3300. support general complex numbers.
  3301.  
  3302.  
  3303. Í(exact->inexact˘ zÍ)                                            ˇprocedure
  3304. Í(inexact->exact˘ zÍ)                                            ˇprocedure
  3305.  
  3306. ÍExact->inexactˇ returns an inexact representation of ˘zˇ. The value returned is the inexact
  3307. number that is numerically closest to the argument. If an exact argument has no reasonably
  3308. close inexact equivalent, then a violation of an implementation restriction may be reported.
  3309.  
  3310. ÍInexact->exactˇ returns an exact representation of ˘zˇ. The value returned is the exact
  3311. number that is numerically closest to the argument. If an inexact argument has no reasonably
  3312. close exact equivalent, then a violation of an implementation restriction may be reported.
  3313.  
  3314. These procedures implement the natural one-to-one correspondence between exact and
  3315. inexact integers throughout an implementation-dependent range. See section 6.5.3.
  3316.  
  3317.  
  3318. ¸6.5.6. Numerical input and outputˇ
  3319.  
  3320.  
  3321. Í(number->string˘ numberÍ)                                ˇessential procedure
  3322. Í(number->string˘ number radixÍ)                           ˇessential procedure
  3323.  
  3324. ˘Radixˇ must be an exact integer, either 2, 8, 10, or 16. If omitted, ˘radixˇ defaults to 10. The
  3325. procedure Ínumber->stringˇ takes a number and a radix and returns as a string an external
  3326. representation of the given number in the given radix such that
  3327.  
  3328. Í    (let ((number ˘numberÍ)
  3329. Í          (radix ˘radixÍ))
  3330. Í      (eqv? number
  3331. Í            (string->number (number->string number
  3332. Í                                            radix)
  3333. Í                            radix)))
  3334. ͡is true. It is an error if no possible result makes this expression true.
  3335.  
  3336. If ˘numberˇ is inexact, the radix is 10, and the above expression can be satisfied by a result
  3337. that contains a decimal point, then the result contains a decimal point and is expressed using
  3338. the minimum number of digits (exclusive of exponent and trailing zeroes) needed to make the
  3339. above expression true [howtoprint] [howtoread]; otherwise the format of the result is
  3340. unspecified.
  3341.  
  3342. The result returned by Ínumber->stringˇ never contains an explicit radix prefix.
  3343.  
  3344. ˘Note:ˇ The error case can occur only when ˘numberˇ is not a complex number or is a complex
  3345. number with a non-rational real or imaginary part.
  3346.  
  3347. ˘Rationale:ˇ If ˘numberˇ is an inexact number represented using flonums, and the radix is 10,
  3348. then the above expression is normally satisfied by a result containing a decimal point. The
  3349. unspecified case allows for infinities, NaNs, and non-flonum representations.
  3350.  
  3351.  
  3352. Í(string->number˘ stringÍ)                                 ˇessential procedure
  3353. Í(string->number˘ string radixÍ)                            ˇessential procedure
  3354.  
  3355. Returns a number of the maximally precise representation expressed by the given ˘stringˇ.
  3356. ˘Radixˇ must be an exact integer, either 2, 8, 10, or 16. If supplied, ˘radixˇ is a default radix
  3357. that may be overridden by an explicit radix prefix in ˘stringˇ (e.g. Í"#o177"ˇ). If ˘radixˇ is not
  3358. supplied, then the default radix is 10. If ˘stringˇ is not a syntactically valid notation for a
  3359. number, then Ístring->numberˇ returns Í#fˇ.
  3360.  
  3361. Í    (string->number "100")      ==>  100
  3362. Í    (string->number "100" 16)   ==>  256
  3363. Í    (string->number "1e2")      ==>  100.0
  3364. Í    (string->number "15##")     ==>  1500.0ˇ
  3365.  
  3366. ˘Note:ˇ Although Ístring->numberˇ is an essential procedure, an implementation may restrict
  3367. its domain in the following ways. ÍString->numberˇ is permitted to return Í#fˇ whenever
  3368. ˘stringˇ contains an explicit radix prefix. If all numbers supported by an implementation are
  3369. real, then Ístring->numberˇ is permitted to return Í#fˇ whenever ˘stringˇ uses the polar or
  3370. rectangular notations for complex numbers. If all numbers are integers, then
  3371. Ístring->numberˇ may return Í#fˇ whenever the fractional notation is used. If all numbers
  3372. are exact, then Ístring->numberˇ may return Í#fˇ whenever an exponent marker or explicit
  3373. exactness prefix is used, or if a Í#ˇ appears in place of a digit. If all inexact numbers are
  3374. integers, then Ístring->numberˇ may return Í#fˇ whenever a decimal point is used.
  3375.  
  3376.  
  3377. Û6.6. Charactersˇ
  3378.  
  3379. Characters are objects that represent printed characters such as letters and digits.
  3380. Characters are written using the notation Í#\ˇ<character> or Í#\ˇ<character name>. For
  3381. example:
  3382.  
  3383.     
  3384.     Í#\a          ˇ; lower case letter
  3385.     Í#\A          ˇ; upper case letter
  3386.     Í#\(          ˇ; left parenthesis
  3387.     Í#\           ˇ; the space character
  3388.     Í#\space      ˇ; the preferred way to write a space
  3389.     Í#\newline    ˇ; the newline character
  3390.  
  3391.  
  3392. Case is significant in Í#\ˇ<character>, but not in Í#\ˇ<character name>. If <character> in
  3393. Í#\ˇ<character> is alphabetic, then the character following <character> must be a delimiter
  3394. character such as a space or parenthesis. This rule resolves the ambiguous case where, for
  3395. example, the sequence of characters ``Í#\spaceˇ'' could be taken to be either a representation
  3396. of the space character or a representation of the character ``Í#\sˇ'' followed by a
  3397. representation of the symbol ``Ípaceˇ.''
  3398.  
  3399. Characters written in the Í#\ˇ notation are self-evaluating. That is, they do not have to be
  3400. quoted in programs.
  3401.  
  3402. Some of the procedures that operate on characters ignore the difference between upper case
  3403. and lower case. The procedures that ignore case have ``Í-ciˇ'' (for ``case insensitive'')
  3404. embedded in their names.
  3405.  
  3406.  
  3407. Í(char?˘ objÍ)                                            ˇessential procedure
  3408.  
  3409. Returns Í#tˇ if ˘objˇ is a character, otherwise returns Í#fˇ.
  3410.  
  3411.  
  3412. Í(char=?˘ char¯1˘ char¯2Í)                                    ˇessential procedure
  3413. Í(char<?˘ char¯1˘ char¯2Í)                                    ˇessential procedure
  3414. Í(char>?˘ char¯1˘ char¯2Í)                                    ˇessential procedure
  3415. Í(char<=?˘ char¯1˘ char¯2Í)                                   ˇessential procedure
  3416. Í(char>=?˘ char¯1˘ char¯2Í)                                   ˇessential procedure
  3417.  
  3418. These procedures impose a total ordering on the set of characters. It is guaranteed that
  3419. under this ordering:
  3420.  
  3421. • The upper case characters are in order. For example, Í(char<? #\A #\B)ˇ returns Í#tˇ.
  3422.  
  3423. • The lower case characters are in order. For example, Í(char<? #\a #\b)ˇ returns Í#tˇ.
  3424.  
  3425. • The digits are in order. For example, Í(char<? #\0 #\9)ˇ returns Í#tˇ.
  3426.  
  3427. • Either all the digits precede all the upper case letters, or vice versa.
  3428.  
  3429. • Either all the digits precede all the lower case letters, or vice versa.
  3430.  
  3431. Some implementations may generalize these procedures to take more than two arguments, as
  3432. with the corresponding numerical predicates.
  3433.  
  3434.  
  3435. Í(char-ci=?˘ char¯1˘ char¯2Í)                                 ˇessential procedure
  3436. Í(char-ci<?˘ char¯1˘ char¯2Í)                                 ˇessential procedure
  3437. Í(char-ci>?˘ char¯1˘ char¯2Í)                                 ˇessential procedure
  3438. Í(char-ci<=?˘ char¯1˘ char¯2Í)                                ˇessential procedure
  3439. Í(char-ci>=?˘ char¯1˘ char¯2Í)                                ˇessential procedure
  3440.  
  3441. These procedures are similar to Íchar=?ˇ et cetera, but they treat upper case and lower case
  3442. letters as the same. For example, Í(char-ci=? #\A #\a)ˇ returns Í#tˇ. Some
  3443. implementations may generalize these procedures to take more than two arguments, as with
  3444. the corresponding numerical predicates.
  3445.  
  3446.  
  3447. Í(char-alphabetic?˘ charÍ)                                ˇessential procedure
  3448. Í(char-numeric?˘ charÍ)                                   ˇessential procedure
  3449. Í(char-whitespace?˘ charÍ)                                ˇessential procedure
  3450. Í(char-upper-case?˘ letterÍ)                               ˇessential procedure
  3451. Í(char-lower-case?˘ letterÍ)                               ˇessential procedure
  3452.  
  3453. These procedures return Í#tˇ if their arguments are alphabetic, numeric, whitespace, upper
  3454. case, or lower case characters, respectively, otherwise they return Í#fˇ. The following
  3455. remarks, which are specific to the ASCII character set, are intended only as a guide: The
  3456. alphabetic characters are the 52 upper and lower case letters. The numeric characters are
  3457. the ten decimal digits. The whitespace characters are space, tab, line feed, form feed, and
  3458. carriage return.
  3459.  
  3460.  
  3461. Í(char->integer˘ charÍ)                                   ˇessential procedure
  3462. Í(integer->char˘ nÍ)                                      ˇessential procedure
  3463.  
  3464. Given a character, Íchar->integerˇ returns an exact integer representation of the
  3465. character. Given an exact integer that is the image of a character under Íchar->integerˇ,
  3466. Íinteger->charˇ returns that character. These procedures implement injective order
  3467. isomorphisms between the set of characters under the Íchar<=?ˇ ordering and some subset of
  3468. the integers under the Í<=ˇ ordering. That is, if
  3469.  
  3470. Í    (char<=? ˘aÍ ˘bÍ) ==> #t  and  (<= ˘xÍ ˘yÍ) ==> #tˇ
  3471.  
  3472. and ˘xˇ and ˘yˇ are in the domain of Íinteger->charˇ, then
  3473.  
  3474. Í    (<= (char->integer ˘aÍ)
  3475. Í        (char->integer ˘bÍ))      ==>  #t
  3476. Í    
  3477. Í    (char<=? (integer->char ˘xÍ)
  3478. Í             (integer->char ˘yÍ)) ==>  #tˇ
  3479.  
  3480.  
  3481. Í(char-upcase˘ charÍ)                                     ˇessential procedure
  3482. Í(char-downcase˘ charÍ)                                   ˇessential procedure
  3483.  
  3484. These procedures return a character ˘char¯2ˇ such that Í(char-ci=? ˘charÍ ˘char¯2Í)ˇ. In
  3485. addition, if ˘charˇ is alphabetic, then the result of Íchar-upcaseˇ is upper case and the result
  3486. of Íchar-downcaseˇ is lower case.
  3487.  
  3488.  
  3489. Û6.7. Stringsˇ
  3490.  
  3491. Strings are sequences of characters. Strings are written as sequences of characters
  3492. enclosed within doublequotes (Í"ˇ). A doublequote can be written inside a string only by
  3493. escaping it with a backslash (Í\ˇ), as in
  3494.  
  3495. Í    "The word \"recursion\" has many meanings."ˇ
  3496.  
  3497. A backslash can be written inside a string only by escaping it with another backslash.
  3498. Scheme does not specify the effect of a backslash within a string that is not followed by a
  3499. doublequote or backslash.
  3500.  
  3501. A string constant may continue from one line to the next, but the exact contents of such a
  3502. string are unspecified.
  3503.  
  3504. The ˘lengthˇ of a string is the number of characters that it contains. This number is a
  3505. non-negative integer that is fixed when the string is created. The ˘valid indexesˇ of a string
  3506. are the exact non-negative integers less than the length of the string. The first character of
  3507. a string has index 0, the second has index 1, and so on.
  3508.  
  3509. In phrases such as ``the characters of ˘stringˇ beginning with index ˘startˇ and ending with index
  3510. ˘endˇ,'' it is understood that the index ˘startˇ is inclusive and the index ˘endˇ is exclusive. Thus if
  3511. ˘startˇ and ˘endˇ are the same index, a null substring is referred to, and if ˘startˇ is zero and ˘endˇ
  3512. is the length of ˘stringˇ, then the entire string is referred to.
  3513.  
  3514. Some of the procedures that operate on strings ignore the difference between upper and
  3515. lower case. The versions that ignore case have ``Í-ciˇ'' (for ``case insensitive'') embedded in
  3516. their names.
  3517.  
  3518.  
  3519. Í(string?˘ objÍ)                                          ˇessential procedure
  3520.  
  3521. Returns Í#tˇ if ˘objˇ is a string, otherwise returns Í#fˇ.
  3522.  
  3523.  
  3524. Í(make-string˘ kÍ)                                        ˇessential procedure
  3525. Í(make-string˘ k charÍ)                                    ˇessential procedure
  3526.  
  3527. ÍMake-stringˇ returns a newly allocated string of length ˘kˇ. If ˘charˇ is given, then all
  3528. elements of the string are initialized to ˘charˇ, otherwise the contents of the ˘stringˇ are
  3529. unspecified.
  3530.  
  3531.  
  3532. Í(string˘ char ...Í)                                        ˇessential procedure
  3533.  
  3534. Returns a newly allocated string composed of the arguments.
  3535.  
  3536.  
  3537. Í(string-length˘ stringÍ)                                  ˇessential procedure
  3538.  
  3539. Returns the number of characters in the given ˘stringˇ.
  3540.  
  3541.  
  3542. Í(string-ref˘ string kÍ)                                    ˇessential procedure
  3543.  
  3544. ˘kˇ must be a valid index of ˘stringˇ. ÍString-refˇ returns character ˘kˇ of ˘stringˇ using
  3545. zero-origin indexing.
  3546.  
  3547.  
  3548. Í(string-set!˘ string k charÍ)                               ˇessential procedure
  3549.  
  3550. ˘kˇ must be a valid index of ˘stringˇ. ÍString-set!ˇ stores ˘charˇ in element ˘kˇ of ˘stringˇ and
  3551. returns an unspecified value.
  3552.  
  3553. Í    (define (f) (make-string 3 #\*))
  3554. Í    (define (g) "***")
  3555. Í    (string-set! (f) 0 #\?)     ==>  ˘unspecifiedÍ
  3556. Í    (string-set! (g) 0 #\?)     ==>  ˘errorÍ
  3557. Í    (string-set! (symbol->string 'immutable)
  3558. Í                 0
  3559. Í                 #\?)           ==>  ˘errorˇ
  3560.  
  3561.  
  3562. Í(string=?˘ string¯1˘ string¯2Í)                                ˇessential procedure
  3563. Í(string-ci=?˘ string¯1˘ string¯2Í)                             ˇessential procedure
  3564.  
  3565. Returns Í#tˇ if the two strings are the same length and contain the same characters in the
  3566. same positions, otherwise returns Í#fˇ. ÍString-ci=?ˇ treats upper and lower case letters as
  3567. though they were the same character, but Ístring=?ˇ treats upper and lower case as distinct
  3568. characters.
  3569.  
  3570.  
  3571. Í(string<?˘ string¯1˘ string¯2Í)                                ˇessential procedure
  3572. Í(string>?˘ string¯1˘ string¯2Í)                                ˇessential procedure
  3573. Í(string<=?˘ string¯1˘ string¯2Í)                               ˇessential procedure
  3574. Í(string>=?˘ string¯1˘ string¯2Í)                               ˇessential procedure
  3575. Í(string-ci<?˘ string¯1˘ string¯2Í)                             ˇessential procedure
  3576. Í(string-ci>?˘ string¯1˘ string¯2Í)                             ˇessential procedure
  3577. Í(string-ci<=?˘ string¯1˘ string¯2Í)                            ˇessential procedure
  3578. Í(string-ci>=?˘ string¯1˘ string¯2Í)                            ˇessential procedure
  3579.  
  3580. These procedures are the lexicographic extensions to strings of the corresponding orderings
  3581. on characters. For example, Ístring<?ˇ is the lexicographic ordering on strings induced by
  3582. the ordering Íchar<?ˇ on characters. If two strings differ in length but are the same up to the
  3583. length of the shorter string, the shorter string is considered to be lexicographically less
  3584. than the longer string.
  3585.  
  3586. Implementations may generalize these and the Ístring=?ˇ and Ístring-ci=?ˇ procedures to
  3587. take more than two arguments, as with the corresponding numerical predicates.
  3588.  
  3589.  
  3590. Í(substring˘ string start endÍ)                               ˇessential procedure
  3591.  
  3592. ˘Stringˇ must be a string, and ˘startˇ and ˘endˇ must be exact integers satisfying
  3593.  
  3594.     0 <= ˘startˇ <= ˘endˇ <= Í(string-length ˘stringÍ).ˇ
  3595.  
  3596. ÍSubstringˇ returns a newly allocated string formed from the characters of ˘stringˇ beginning
  3597. with index ˘startˇ (inclusive) and ending with index ˘endˇ (exclusive).
  3598.  
  3599.  
  3600. Í(string-append˘ string ...Í)                                ˇessential procedure
  3601.  
  3602. Returns a newly allocated string whose characters form the concatenation of the given
  3603. strings.
  3604.  
  3605.  
  3606. Í(string->list˘ stringÍ)                                   ˇessential procedure
  3607. Í(list->string˘ charsÍ)                                   ˇessential procedure
  3608.  
  3609. ÍString->listˇ returns a newly allocated list of the characters that make up the given
  3610. string. ÍList->stringˇ returns a newly allocated string formed from the characters in the
  3611. list ˘charsˇ. ÍString->listˇ and Ílist->stringˇ are inverses so far as Íequal?ˇ is concerned.
  3612.  
  3613.  
  3614. Í(string-copy˘ stringÍ)                                           ˇprocedure
  3615.  
  3616. Returns a newly allocated copy of the given ˘stringˇ.
  3617.  
  3618.  
  3619. Í(string-fill!˘ string charÍ)                                      ˇprocedure
  3620.  
  3621. Stores ˘charˇ in every element of the given ˘stringˇ and returns an unspecified value.
  3622.  
  3623.  
  3624. Û6.8. Vectorsˇ
  3625.  
  3626. Vectors are heterogenous structures whose elements are indexed by integers. A vector
  3627. typically occupies less space than a list of the same length, and the average time required to
  3628. access a randomly chosen element is typically less for the vector than for the list.
  3629.  
  3630. The ˘lengthˇ of a vector is the number of elements that it contains. This number is a
  3631. non-negative integer that is fixed when the vector is created. The ˘valid indexesˇ of a vector
  3632. are the exact non-negative integers less than the length of the vector. The first element in a
  3633. vector is indexed by zero, and the last element is indexed by one less than the length of the
  3634. vector.
  3635.  
  3636. Vectors are written using the notation Í#(˘objÍ ...)ˇ. For example, a vector of length 3
  3637. containing the number zero in element 0, the list Í(2 2 2 2)ˇ in element 1, and the string
  3638. Í"Anna"ˇ in element 2 can be written as following:
  3639.  
  3640. Í    #(0 (2 2 2 2) "Anna")ˇ
  3641.  
  3642. Note that this is the external representation of a vector, not an expression evaluating to a
  3643. vector. Like list constants, vector constants must be quoted:
  3644.  
  3645. Í    '#(0 (2 2 2 2) "Anna")  
  3646. Í              ==>  #(0 (2 2 2 2) "Anna")ˇ
  3647.  
  3648.  
  3649. Í(vector?˘ objÍ)                                          ˇessential procedure
  3650.  
  3651. Returns Í#tˇ if ˘objˇ is a vector, otherwise returns Í#fˇ.
  3652.  
  3653.  
  3654. Í(make-vector˘ kÍ)                                        ˇessential procedure
  3655. Í(make-vector˘ k fillÍ)                                            ˇprocedure
  3656.  
  3657. Returns a newly allocated vector of ˘kˇ elements. If a second argument is given, then each
  3658. element is initialized to ˘fillˇ. Otherwise the initial contents of each element is unspecified.
  3659.  
  3660.  
  3661. Í(vector˘ obj ...Í)                                         ˇessential procedure
  3662.  
  3663. Returns a newly allocated vector whose elements contain the given arguments. Analogous to
  3664. Ílistˇ.
  3665.  
  3666. Í    (vector 'a 'b 'c)           ==>  #(a b c)ˇ
  3667.  
  3668.  
  3669. Í(vector-length˘ vectorÍ)                                 ˇessential procedure
  3670.  
  3671. Returns the number of elements in ˘vectorˇ.
  3672.  
  3673.  
  3674. Í(vector-ref˘ vector kÍ)                                   ˇessential procedure
  3675.  
  3676. ˘kˇ must be a valid index of ˘vectorˇ. ÍVector-refˇ returns the contents of element ˘kˇ of ˘vectorˇ.
  3677.  
  3678. Í    (vector-ref '#(1 1 2 3 5 8 13 21)
  3679. Í                5)  
  3680. Í              ==>  8
  3681. Í    (vector-ref '#(1 1 2 3 5 8 13 21)
  3682. Í                (inexact->exact
  3683. Í                  (round (* 2 (acos -1))))) 
  3684. Í              ==> 13ˇ
  3685.  
  3686.  
  3687. Í(vector-set!˘ vector k objÍ)                               ˇessential procedure
  3688.  
  3689. ˘kˇ must be a valid index of ˘vectorˇ. ÍVector-set!ˇ stores ˘objˇ in element ˘kˇ of ˘vectorˇ. The value
  3690. returned by Ívector-set!ˇ is unspecified.
  3691.  
  3692. Í    (let ((vec (vector 0 '(2 2 2 2) "Anna")))
  3693. Í      (vector-set! vec 1 '("Sue" "Sue"))
  3694. Í      vec)      
  3695. Í              ==>  #(0 ("Sue" "Sue") "Anna")
  3696. Í    
  3697. Í    (vector-set! '#(0 1 2) 1 "doe")  
  3698. Í              ==>  ˘errorÍ  ; constant vectorˇ
  3699.  
  3700.  
  3701. Í(vector->list˘ vectorÍ)                                  ˇessential procedure
  3702. Í(list->vector˘ listÍ)                                     ˇessential procedure
  3703.  
  3704. ÍVector->listˇ returns a newly allocated list of the objects contained in the elements of
  3705. ˘vectorˇ. ÍList->vectorˇ returns a newly created vector initialized to the elements of the list
  3706. ˘listˇ.
  3707.  
  3708. Í    (vector->list '#(dah dah didah))  
  3709. Í              ==>  (dah dah didah)
  3710. Í    (list->vector '(dididit dah))   
  3711. Í              ==>  #(dididit dah)ˇ
  3712.  
  3713.  
  3714. Í(vector-fill!˘ vector fillÍ)                                       ˇprocedure
  3715.  
  3716. Stores ˘fillˇ in every element of ˘vectorˇ. The value returned by Ívector-fill!ˇ is unspecified.
  3717.  
  3718.  
  3719. Û6.9. Control featuresˇ
  3720.  
  3721. This chapter describes various primitive procedures which control the flow of program
  3722. execution in special ways. The Íprocedure?ˇ predicate is also described here.
  3723.  
  3724.  
  3725. Í(procedure?˘ objÍ)                                       ˇessential procedure
  3726.  
  3727. Returns Í#tˇ if ˘objˇ is a procedure, otherwise returns Í#fˇ.
  3728.  
  3729. Í    (procedure? car)            ==>  #t
  3730. Í    (procedure? 'car)           ==>  #f
  3731. Í    (procedure? (lambda (x) (* x x)))
  3732. Í                                ==>  #t
  3733. Í    (procedure? '(lambda (x) (* x x)))
  3734. Í                                ==>  #f
  3735. Í    (call-with-current-continuation procedure?)
  3736. Í                                ==>  #tˇ
  3737.  
  3738.  
  3739. Í(apply˘ proc argsÍ)                                       ˇessential procedure
  3740. Í(apply˘ proc arg¯1˘ ... argsÍ)                                        ˇprocedure
  3741.  
  3742. ˘Procˇ must be a procedure and ˘argsˇ must be a list. The first (essential) form calls ˘procˇ with
  3743. the elements of ˘argsˇ as the actual arguments. The second form is a generalization of the first
  3744. that calls ˘procˇ with the elements of the list Í(append (list ˘arg¯1Í ...) ˘argsÍ)ˇ as the
  3745. actual arguments.
  3746.  
  3747. Í    (apply + (list 3 4))        ==>  7
  3748. Í    
  3749. Í    (define compose
  3750. Í      (lambda (f g)
  3751. Í        (lambda args
  3752. Í          (f (apply g args)))))
  3753. Í    
  3754. Í    ((compose sqrt *) 12 75)    ==>  30ˇ
  3755.  
  3756.  
  3757. Í(map˘ proc list¯1˘ list¯2˘ ...Í)                                   ˇessential procedure
  3758.  
  3759. The ˘listˇs must be lists, and ˘procˇ must be a procedure taking as many arguments as there are
  3760. ˘listˇs. If more than one ˘listˇ is given, then they must all be the same length. ÍMapˇ applies ˘procˇ
  3761. element-wise to the elements of the ˘listˇs and returns a list of the results, in order from left
  3762. to right. The dynamic order in which ˘procˇ is applied to the elements of the ˘listˇs is
  3763. unspecified.
  3764.  
  3765. Í    (map cadr '((a b) (d e) (g h)))   
  3766. Í              ==>  (b e h)
  3767. Í    
  3768. Í    (map (lambda (n) (expt n n))
  3769. Í         '(1 2 3 4 5))                
  3770. Í              ==>  (1 4 27 256 3125)
  3771. Í    
  3772. Í    (map + '(1 2 3) '(4 5 6))   ==>  (5 7 9)
  3773. Í    
  3774. Í    (let ((count 0))
  3775. Í      (map (lambda (ignored)
  3776. Í             (set! count (+ count 1))
  3777. Í             count)
  3778. Í           '(a b c)))           ==>  ˘unspecifiedˇ
  3779.  
  3780.  
  3781. Í(for-each˘ proc list¯1˘ list¯2˘ ...Í)                              ˇessential procedure
  3782.  
  3783. The arguments to Ífor-eachˇ are like the arguments to Ímapˇ, but Ífor-eachˇ calls ˘procˇ for its
  3784. side effects rather than for its values. Unlike Ímapˇ, Ífor-eachˇ is guaranteed to call ˘procˇ on
  3785. the elements of the ˘listˇs in order from the first element to the last, and the value returned
  3786. by Ífor-eachˇ is unspecified.
  3787.  
  3788. Í    (let ((v (make-vector 5)))
  3789. Í      (for-each (lambda (i)
  3790. Í                  (vector-set! v i (* i i)))
  3791. Í                '(0 1 2 3 4))
  3792. Í      v)                        ==>  #(0 1 4 9 16)ˇ
  3793.  
  3794.  
  3795. Í(force˘ promiseÍ)                                                ˇprocedure
  3796.  
  3797. Forces the value of ˘promiseˇ (see Ídelayˇ, section 4.2.5). If no value has been computed for
  3798. the promise, then a value is computed and returned. The value of the promise is cached (or
  3799. ``memoized'') so that if it is forced a second time, the previously computed value is
  3800. returned.
  3801.  
  3802. Í    (force (delay (+ 1 2)))     ==>  3
  3803. Í    (let ((p (delay (+ 1 2))))
  3804. Í      (list (force p) (force p)))  
  3805. Í                                ==>  (3 3)
  3806. Í    
  3807. Í    (define a-stream
  3808. Í      (letrec ((next
  3809. Í                (lambda (n)
  3810. Í                  (cons n (delay (next (+ n 1)))))))
  3811. Í        (next 0)))
  3812. Í    (define head car)
  3813. Í    (define tail
  3814. Í      (lambda (stream) (force (cdr stream))))
  3815. Í    
  3816. Í    (head (tail (tail a-stream)))  
  3817. Í                                ==>  2ˇ
  3818.  
  3819. ÍForceˇ and Ídelayˇ are mainly intended for programs written in functional style. The
  3820. following examples should not be considered to illustrate good programming style, but they
  3821. illustrate the property that only one value is computed for a promise, no matter how many
  3822. times it is forced.
  3823.  
  3824. Í    (define count 0)
  3825. Í    (define p
  3826. Í      (delay (begin (set! count (+ count 1))
  3827. Í                    (if (> count x)
  3828. Í                        count
  3829. Í                        (force p)))))
  3830. Í    (define x 5)
  3831. Í    p                           ==>  ˘a promiseÍ
  3832. Í    (force p)                   ==>  6
  3833. Í    p                           ==>  ˘a promise, stillÍ
  3834. Í    (begin (set! x 10)
  3835. Í           (force p))           ==>  6ˇ
  3836.  
  3837. Here is a possible implementation of Ídelayˇ and Íforceˇ. Promises are implemented here as
  3838. procedures of no arguments, and Íforceˇ simply calls its argument:
  3839.  
  3840. Í    (define force
  3841. Í      (lambda (object)
  3842. Í        (object)))ˇ
  3843.  
  3844. We define the expression
  3845.  
  3846. Í    (delay ˇ<expression>Í)ˇ
  3847.  
  3848. to have the same meaning as the procedure call
  3849.  
  3850. Í    (make-promise (lambda () ˇ<expression>Í)),ˇ
  3851.  
  3852. where Ímake-promiseˇ is defined as follows:
  3853.  
  3854. Í    (define make-promise
  3855. Í      (lambda (proc)
  3856. Í        (let ((result-ready? #f)
  3857. Í              (result #f))
  3858. Í          (lambda ()
  3859. Í            (if result-ready?
  3860. Í                result
  3861. Í                (let ((x (proc)))
  3862. Í                  (if result-ready?
  3863. Í                      result
  3864. Í                      (begin (set! result-ready? #t)
  3865. Í                             (set! result x)
  3866. Í                             result))))))))ˇ
  3867.  
  3868. ˘Rationale:ˇ A promise may refer to its own value, as in the last example above. Forcing such
  3869. a promise may cause the promise to be forced a second time before the value of the first
  3870. force has been computed. This complicates the definition of Ímake-promiseˇ.
  3871.  
  3872. Various extensions to this semantics of Ídelayˇ and Íforceˇ are supported in some
  3873. implementations:
  3874.  
  3875. • Calling Íforceˇ on an object that is not a promise may simply return the object.
  3876.  
  3877. • It may be the case that there is no means by which a promise can be operationally
  3878. distinguished from its forced value. That is, expressions like the following may evaluate to
  3879. either Í#tˇ or to Í#fˇ, depending on the implementation:
  3880.  
  3881. Í    (eqv? (delay 1) 1)          ==>  ˘unspecifiedÍ
  3882. Í    (pair? (delay (cons 1 2)))  ==>  ˘unspecifiedˇ
  3883.  
  3884. • Some implementations may implement ``implicit forcing,'' where the value of a promise is
  3885. forced by primitive procedures like Ícdrˇ and Í+ˇ:
  3886.  
  3887. Í    (+ (delay (* 3 7)) 13)      ==>  34ˇ
  3888.  
  3889.  
  3890. Í(call-with-current-continuation˘ procÍ)                  ˇessential procedure
  3891.  
  3892. ˘Procˇ must be a procedure of one argument. The procedure
  3893. Ícall-with-current-continuationˇ packages up the current continuation (see the
  3894. rationale below) as an ``escape procedure'' and passes it as an argument to ˘procˇ. The escape
  3895. procedure is a Scheme procedure of one argument that, if it is later passed a value, will
  3896. ignore whatever continuation is in effect at that later time and will give the value instead to
  3897. the continuation that was in effect when the escape procedure was created.
  3898.  
  3899. The escape procedure that is passed to ˘procˇ has unlimited extent just like any other
  3900. procedure in Scheme. It may be stored in variables or data structures and may be called as
  3901. many times as desired.
  3902.  
  3903. The following examples show only the most common uses of
  3904. Ícall-with-current-continuationˇ. If all real programs were as simple as these
  3905. examples, there would be no need for a procedure with the power of
  3906. Ícall-with-current-continuationˇ.
  3907.  
  3908. Í    (call-with-current-continuation
  3909. Í      (lambda (exit)
  3910. Í        (for-each (lambda (x)
  3911. Í                    (if (negative? x)
  3912. Í                        (exit x)))
  3913. Í                  '(54 0 37 -3 245 19))
  3914. Í        #t))                    ==>  -3
  3915. Í    
  3916. Í    (define list-length
  3917. Í      (lambda (obj)
  3918. Í        (call-with-current-continuation
  3919. Í          (lambda (return)
  3920. Í            (letrec ((r
  3921. Í                      (lambda (obj)
  3922. Í                        (cond ((null? obj) 0)
  3923. Í                              ((pair? obj)
  3924. Í                               (+ (r (cdr obj)) 1))
  3925. Í                              (else (return #f))))))
  3926. Í              (r obj))))))
  3927. Í    
  3928. Í    (list-length '(1 2 3 4))    ==>  4
  3929. Í    
  3930. Í    (list-length '(a b . c))    ==>  #fˇ
  3931.  
  3932. ˘Rationale:ˇ
  3933.  
  3934. A common use of Ícall-with-current-continuationˇ is for structured, non-local exits
  3935. from loops or procedure bodies, but in fact Ícall-with-current-continuationˇ is
  3936. extremely useful for implementing a wide variety of advanced control structures.
  3937.  
  3938. Whenever a Scheme expression is evaluated there is a ˘continuationˇ wanting the result of the
  3939. expression. The continuation represents an entire (default) future for the computation. If the
  3940. expression is evaluated at top level, for example, then the continuation might take the
  3941. result, print it on the screen, prompt for the next input, evaluate it, and so on forever. Most
  3942. of the time the continuation includes actions specified by user code, as in a continuation that
  3943. will take the result, multiply it by the value stored in a local variable, add seven, and give
  3944. the answer to the top level continuation to be printed. Normally these ubiquitous
  3945. continuations are hidden behind the scenes and programmers don't think much about them. On
  3946. rare occasions, however, a programmer may need to deal with continuations explicitly.
  3947. ÍCall-with-current-continuationˇ allows Scheme programmers to do that by creating
  3948. a procedure that acts just like the current continuation.
  3949.  
  3950. Most programming languages incorporate one or more special-purpose escape constructs
  3951. with names like Íexitˇ, Íreturnˇ, or even Ígotoˇ. In 1965, however, Peter Landin [Landin]
  3952. invented a general purpose escape operator called the J-operator. John Reynolds [Reynolds]
  3953. described a simpler but equally powerful construct in 1972. The Ícatchˇ special form
  3954. described by Sussman and Steele in the 1975 report on Scheme is exactly the same as
  3955. Reynolds's construct, though its name came from a less general construct in MacLisp.
  3956. Several Scheme implementors noticed that the full power of the Ícatchˇ construct could be
  3957. provided by a procedure instead of by a special syntactic construct, and the name
  3958. Ícall-with-current-continuationˇ was coined in 1982. This name is descriptive, but
  3959. opinions differ on the merits of such a long name, and some people use the name Ícall/ccˇ
  3960. instead.
  3961.  
  3962.  
  3963. Û6.10. Input and outputˇ
  3964.  
  3965.  
  3966. ¸6.10.1. Portsˇ
  3967.  
  3968. Ports represent input and output devices. To Scheme, an input port is a Scheme object that
  3969. can deliver characters upon command, while an output port is a Scheme object that can
  3970. accept characters.
  3971.  
  3972.  
  3973. Í(call-with-input-file˘ string procÍ)                       ˇessential procedure
  3974. Í(call-with-output-file˘ string procÍ)                      ˇessential procedure
  3975.  
  3976. ˘Procˇ should be a procedure of one argument, and ˘stringˇ should be a string naming a file. For
  3977. Ícall-with-input-fileˇ, the file must already exist; for Ícall-with-output-fileˇ, the
  3978. effect is unspecified if the file already exists. These procedures call ˘procˇ with one
  3979. argument: the port obtained by opening the named file for input or output. If the file cannot be
  3980. opened, an error is signalled. If the procedure returns, then the port is closed automatically
  3981. and the value yielded by the procedure is returned. If the procedure does not return, then the
  3982. port will not be closed automatically unless it is possible to prove that the port will never
  3983. again be used for a read or write operation.
  3984.  
  3985. ˘Rationale:ˇ Because Scheme's escape procedures have unlimited extent, it is possible to
  3986. escape from the current continuation but later to escape back in. If implementations were
  3987. permitted to close the port on any escape from the current continuation, then it would be
  3988. impossible to write portable code using both Ícall-with-current-continuationˇ and
  3989. Ícall-with-input-fileˇ or Ícall-with-output-fileˇ.
  3990.  
  3991.  
  3992. Í(input-port?˘ objÍ)                                      ˇessential procedure
  3993. Í(output-port?˘ objÍ)                                     ˇessential procedure
  3994.  
  3995. Returns Í#tˇ if ˘objˇ is an input port or output port respectively, otherwise returns Í#fˇ.
  3996.  
  3997.  
  3998. Í(current-input-port)                                  ˇessential procedure
  3999. Í(current-output-port)                                 ˇessential procedure
  4000.  
  4001. Returns the current default input or output port.
  4002.  
  4003.  
  4004. Í(with-input-from-file˘ string thunkÍ)                              ˇprocedure
  4005. Í(with-output-to-file˘ string thunkÍ)                               ˇprocedure
  4006.  
  4007. ˘Thunkˇ must be a procedure of no arguments, and ˘stringˇ must be a string naming a file. For
  4008. Íwith-input-from-fileˇ, the file must already exist; for Íwith-output-to-fileˇ, the
  4009. effect is unspecified if the file already exists. The file is opened for input or output, an input
  4010. or output port connected to it is made the default value returned by Ícurrent-input-portˇ
  4011. or Ícurrent-output-portˇ, and the ˘thunkˇ is called with no arguments. When the ˘thunkˇ
  4012. returns, the port is closed and the previous default is restored. ÍWith-input-from-fileˇ
  4013. and Íwith-output-to-fileˇ return the value yielded by ˘thunkˇ. If an escape procedure is
  4014. used to escape from the continuation of these procedures, their behavior is implementation
  4015. dependent.
  4016.  
  4017.  
  4018. Í(open-input-file˘ filenameÍ)                              ˇessential procedure
  4019.  
  4020. Takes a string naming an existing file and returns an input port capable of delivering
  4021. characters from the file. If the file cannot be opened, an error is signalled.
  4022.  
  4023.  
  4024. Í(open-output-file˘ filenameÍ)                             ˇessential procedure
  4025.  
  4026. Takes a string naming an output file to be created and returns an output port capable of
  4027. writing characters to a new file by that name. If the file cannot be opened, an error is
  4028. signalled. If a file with the given name already exists, the effect is unspecified.
  4029.  
  4030.  
  4031. Í(close-input-port˘ portÍ)                                ˇessential procedure
  4032. Í(close-output-port˘ portÍ)                               ˇessential procedure
  4033.  
  4034. Closes the file associated with ˘portˇ, rendering the ˘portˇ incapable of delivering or accepting
  4035. characters. These routines have no effect if the file has already been closed. The value
  4036. returned is unspecified.
  4037.  
  4038.  
  4039. ¸6.10.2. Inputˇ
  4040.  
  4041.  
  4042. Í(read)                                                ˇessential procedure
  4043. Í(read˘ portÍ)                                            ˇessential procedure
  4044.  
  4045. ÍReadˇ converts external representations of Scheme objects into the objects themselves. That
  4046. is, it is a parser for the nonterminal <datum> (see sections 7.1.2 and 6.3). ÍReadˇ returns the
  4047. next object parsable from the given input ˘portˇ, updating ˘portˇ to point to the first character
  4048. past the end of the external representation of the object.
  4049.  
  4050. If an end of file is encountered in the input before any characters are found that can begin an
  4051. object, then an end of file object is returned. The port remains open, and further attempts to
  4052. read will also return an end of file object. If an end of file is encountered after the beginning
  4053. of an object's external representation, but the external representation is incomplete and
  4054. therefore not parsable, an error is signalled.
  4055.  
  4056. The ˘portˇ argument may be omitted, in which case it defaults to the value returned by
  4057. Ícurrent-input-portˇ. It is an error to read from a closed port.
  4058.  
  4059.  
  4060. Í(read-char)                                           ˇessential procedure
  4061. Í(read-char˘ portÍ)                                       ˇessential procedure
  4062.  
  4063. Returns the next character available from the input ˘portˇ, updating the ˘portˇ to point to the
  4064. following character. If no more characters are available, an end of file object is returned.
  4065. ˘Portˇ may be omitted, in which case it defaults to the value returned by
  4066. Ícurrent-input-portˇ.
  4067.  
  4068.  
  4069. Í(peek-char)                                           ˇessential procedure
  4070. Í(peek-char˘ portÍ)                                       ˇessential procedure
  4071.  
  4072. Returns the next character available from the input ˘portˇ, ˘withoutˇ updating the ˘portˇ to point
  4073. to the following character. If no more characters are available, an end of file object is
  4074. returned. ˘Portˇ may be omitted, in which case it defaults to the value returned by
  4075. Ícurrent-input-portˇ.
  4076.  
  4077. ˘Note:ˇ The value returned by a call to Ípeek-charˇ is the same as the value that would have
  4078. been returned by a call to Íread-charˇ with the same ˘portˇ. The only difference is that the
  4079. very next call to Íread-charˇ or Ípeek-charˇ on that ˘portˇ will return the value returned by
  4080. the preceding call to Ípeek-charˇ. In particular, a call to Ípeek-charˇ on an interactive port
  4081. will hang waiting for input whenever a call to Íread-charˇ would have hung.
  4082.  
  4083.  
  4084. Í(eof-object?˘ objÍ)                                      ˇessential procedure
  4085.  
  4086. Returns Í#tˇ if ˘objˇ is an end of file object, otherwise returns Í#fˇ. The precise set of end of file
  4087. objects will vary among implementations, but in any case no end of file object will ever be
  4088. an object that can be read in using Íreadˇ.
  4089.  
  4090.  
  4091. Í(char-ready?)                                                ˇprocedure
  4092. Í(char-ready?˘ portÍ)                                            ˇprocedure
  4093.  
  4094. Returns Í#tˇ if a character is ready on the input ˘portˇ and returns Í#fˇ otherwise. If
  4095. Íchar-readyˇ returns Í#tˇ then the next Íread-charˇ operation on the given ˘portˇ is guaranteed
  4096. not to hang. If the ˘portˇ is at end of file then Íchar-ready?ˇ returns Í#tˇ. ˘Portˇ may be omitted,
  4097. in which case it defaults to the value returned by Ícurrent-input-portˇ.
  4098.  
  4099. ˘Rationale:ˇ ÍChar-ready?ˇ exists to make it possible for a program to accept characters from
  4100. interactive ports without getting stuck waiting for input. Any input editors associated with
  4101. such ports must ensure that characters whose existence has been asserted by
  4102. Íchar-ready?ˇ cannot be rubbed out. If Íchar-ready?ˇ were to return Í#fˇ at end of file, a
  4103. port at end of file would be indistinguishable from an interactive port that has no ready
  4104. characters.
  4105.  
  4106.  
  4107. ¸6.10.3. Outputˇ
  4108.  
  4109.  
  4110. Í(write˘ objÍ)                                            ˇessential procedure
  4111. Í(write˘ obj portÍ)                                        ˇessential procedure
  4112.  
  4113. Writes a written representation of ˘objˇ to the given ˘portˇ. Strings that appear in the written
  4114. representation are enclosed in doublequotes, and within those strings backslash and
  4115. doublequote characters are escaped by backslashes. ÍWriteˇ returns an unspecified value. The
  4116. ˘portˇ argument may be omitted, in which case it defaults to the value returned by
  4117. Ícurrent-output-portˇ.
  4118.  
  4119.  
  4120. Í(display˘ objÍ)                                          ˇessential procedure
  4121. Í(display˘ obj portÍ)                                      ˇessential procedure
  4122.  
  4123. Writes a representation of ˘objˇ to the given ˘portˇ. Strings that appear in the written
  4124. representation are not enclosed in doublequotes, and no characters are escaped within those
  4125. strings. Character objects appear in the representation as if written by Íwrite-charˇ
  4126. instead of by Íwriteˇ. ÍDisplayˇ returns an unspecified value. The ˘portˇ argument may be
  4127. omitted, in which case it defaults to the value returned by Ícurrent-output-portˇ.
  4128.  
  4129. ˘Rationale:ˇ ÍWriteˇ is intended for producing machine-readable output and Ídisplayˇ is for
  4130. producing human-readable output. Implementations that allow ``slashification'' within
  4131. symbols will probably want Íwriteˇ but not Ídisplayˇ to slashify funny characters in
  4132. symbols.
  4133.  
  4134.  
  4135. Í(newline)                                             ˇessential procedure
  4136. Í(newline˘ portÍ)                                         ˇessential procedure
  4137.  
  4138. Writes an end of line to ˘portˇ. Exactly how this is done differs from one operating system to
  4139. another. Returns an unspecified value. The ˘portˇ argument may be omitted, in which case it
  4140. defaults to the value returned by Ícurrent-output-portˇ.
  4141.  
  4142.  
  4143. Í(write-char˘ charÍ)                                      ˇessential procedure
  4144. Í(write-char˘ char portÍ)                                  ˇessential procedure
  4145.  
  4146. Writes the character ˘charˇ (not an external representation of the character) to the given ˘portˇ
  4147. and returns an unspecified value. The ˘portˇ argument may be omitted, in which case it
  4148. defaults to the value returned by Ícurrent-output-portˇ.
  4149.  
  4150.  
  4151. ¸6.10.4. System interfaceˇ
  4152.  
  4153. Questions of system interface generally fall outside of the domain of this report. However,
  4154. the following operations are important enough to deserve description here.
  4155.  
  4156.  
  4157. Í(load˘ filenameÍ)                                         ˇessential procedure
  4158.  
  4159. ˘Filenameˇ should be a string naming an existing file containing Scheme source code. The Íloadˇ
  4160. procedure reads expressions and definitions from the file and evaluates them sequentially. It
  4161. is unspecified whether the results of the expressions are printed. The Íloadˇ procedure does
  4162. not affect the values returned by Ícurrent-input-portˇ and Ícurrent-output-portˇ.
  4163. ÍLoadˇ returns an unspecified value.
  4164.  
  4165. ˘Rationale:ˇ For portability, Íloadˇ must operate on source files. Its operation on other kinds of
  4166. files necessarily varies among implementations.
  4167.  
  4168.  
  4169. Í(transcript-on˘ filenameÍ)                                       ˇprocedure
  4170. Í(transcript-off)                                             ˇprocedure
  4171.  
  4172. ˘Filenameˇ must be a string naming an output file to be created. The effect of Ítranscript-onˇ
  4173. is to open the named file for output, and to cause a transcript of subsequent interaction
  4174. between the user and the Scheme system to be written to the file. The transcript is ended by
  4175. a call to Ítranscript-offˇ, which closes the transcript file. Only one transcript may be in
  4176. progress at any time, though some implementations may relax this restriction. The values
  4177. returned by these procedures are unspecified.
  4178.  
  4179.  
  4180. Û7. Formal syntax and semanticsˇ
  4181.  
  4182. Due to formatting restrictions, this chapter is not included in the online help.
  4183.  
  4184.